Home > Software design >  How to remove a particular field containing certain value using Jolt
How to remove a particular field containing certain value using Jolt

Time:10-25

I have the below JSON structure :

{
  "data": {
    "a1": "value1",
    "a2": "value1",
    "a3": "value1",
    "collection": [
      {
        "events": [
          {
            "x1": 123,
            "x2": "NA",
            "x3": 5678
          },
          {
            "x1": 432,
            "x2": 854,
            "x3": 912
          }
        ]
      }
    ]
  }
}

I would like to remove the field x2 whenever it has value as "NA" or "NV" using Jolt

Desired output :

{
  "data": {
    "a1": "value1",
    "a2": "value1",
    "a3": "value1",
    "collection": [
      {
        "events": [
          {
            "x1": 123,
            "x3": 5678
          },
          {
            "x1": 432,
            "x2": 854,
            "x3": 912
          }
        ]
      }
    ]
  }
}

CodePudding user response:

You can use the following shift transformation spec with conditional logic

[
  {
    "operation": "shift",
    "spec": {
      "data": {
        "*": "&1.&",
        "collection": {
          "*": {
            "events": {
              "*": {
                "x2": {
                  "NA|NV": { // | is OR operator
                    "*": ""
                  },
                  "*": {
                    "@1": "&7.&6[&5].&4[&3].&2" // &7 represents the level of "data", &6 represents the level of "collection", [&5] -> the indexes of it, &4 -> the level of "events" etc.
                  }
                },
                "*": "&5.&4[&3].&2[&1].&" // reduce two level with respect to the above value identifier
              }
            }
          }
        }
      }
    }
  }
]

the demo on the site enter image description here

  • Related