Search code examples
jsonjolt

Jolt Remove Entry if a key is not present


My input JSON is like below

[
  {
    "id": "112",
    "key": "value1",
    "dfsf": "dffd"
  },
  {
    "key1": "value2"
  },
  {
    "id": "113",
    "key2": "value3"
  }
]

If id is not present, I want to remove the entry itself such that my output JSON looks like

[
  {
    "id": "112",
    "key": "value1"
  },
  {
    "id": "113",
    "key2": "value3"
  }
]

Solution

  • You can replicate all the values from the level of id by using a @1 on the left-hand-side nested within the innermost object while filtering it by "id" which's the key of the object such as

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "id": {
              "@1": ""
            }
          }
        }
      }
    ]
    

    the demo on the site https://jolt-demo.appspot.com/ is :

    enter image description here