Search code examples
jsonjolt

Filter The json array using jolt spec


We need to filter the records: if both are the same, then reject; if different, we need to output them

[
  {
    "item": 2,
    "code": 23,
    "modelcode": 23
  },
  {
    "item": 2,
    "code": 23,
    "modelcode": 24
  }
]

output should be

[
  {
    "item": 2,
    "code": 23,
    "modelcode": 24
  }
]

Solution

  • You can check out the equality of both attributes through use of size whenever they're matched one by one, whether equal to 2 which will mean they're different( the value 1 means they're identical ).

    So, you can use the following transformation :

    [
      {// generate new object cmn in order to get whether it has one or two elements
        "operation": "shift",
        "spec": {
          "*": {
            "*": "&1.&",
            "#common": ["&1.cmn.@1,modelcode", "&1.cmn.@1,code"]
          }
        }
      },
      {// measure the sizes for newly generated objects respectively
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": {
            "cmn": "=size"
          }
        }
      },
      {// separate by those sizes 
        "operation": "shift",
        "spec": {
          "*": {
            "*": "@1,cmn.&1.&"
          }
        }
      },
      {// filter by the size 2 in order to get the objects with different values
        "operation": "shift",
        "spec": {
          "2": {
            "*": {
              "*": "[#2].&"
            }
          }
        }
      },
      {// get rid of the extra identifier which is "cmn"
        "operation": "remove",
        "spec": {
          "*": {
            "cmn": ""
          }
        }
      }
    ]
    

    The more straightforward case follows as an alternative :

    [
      { // partition the objects by both of those values
        "operation": "shift",
        "spec": {
          "*": {
            "@": "@1,modelcode.@1,code.&1"
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": {
            "&": { // matching ones
              "*": {
                "": "" // < skip this case >
              }
            },
            "*": { // non-matching ones
              "*": {
                 "@": "[]"
              }
            }
          }
        }
      }
    ]