Home > OS >  How to remove empty object tags
How to remove empty object tags

Time:10-06

Given the following JSON:

{
      "identifers": {
        "_id": "f073895e207c3157",
        "attrsDigest": "-706912700",
        "entityTypes": [
          {
            "_id": "bacf9e903a6c5def",
            "attrsDigest": "1537152651"
          },
          {
            "_id": "08aac63dacd579c8",
            "attrsDigest": "-1461027456"
          }
        ],
        "statuses": [
          {
            "_id": "50912252d4781056",
            "attrsDigest": "154590869"
          }
        ]
      }
    }

I need to produce the following:

{
  "identifers": [
    {
      "_id": "f073895e207c3157",
      "attrsDigest": "-706912700"
    },
    {
        "_id": "bacf9e903a6c5def",
        "attrsDigest": "1537152651"
    },
    {
      "_id": "08aac63dacd579c8",
      "attrsDigest": "-1461027456"
    },
    {
      "_id": "50912252d4781056",
      "attrsDigest": "154590869"
    }
    ]
  }

I've got the following:

[**].${ "identifers": _id, "digest": attrsDigest }

which produces this:

{
  "identifers": [
    "f073895e207c3157",
    "bacf9e903a6c5def",
    "08aac63dacd579c8",
    "50912252d4781056"
  ],
  "digest": [
    "-706912700",
    "1537152651",
    "-1461027456",
    "154590869"
  ]
}

CodePudding user response:

You can use the "reduce" operator to group your nested data by ID and then format it into an array of the desired shape:

**{ _id: attrsDigest }
  ~> $each(function($attrsDigest, $id) {
    { "_id": $id, "attrsDigest": $attrsDigest }
  })

Live playground: https://stedi.link/A1yo9Pz

  • Related