bazaarvoice / jolt

JSON to JSON transformation library written in Java.
Apache License 2.0
1.55k stars 330 forks source link

squashNulls and recursivelySquashNulls need to work on arrays #1255

Open samer1977 opened 4 months ago

samer1977 commented 4 months ago

Hi,

There should be a way to make the squashNulls \recursivelySquashNulls remove null objects from an array.

Example:

Input:

{
  "items": [
    1,
    null,
    2
  ]
}

Spec:

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "items": "=recursivelySquashNulls" //squashNulls same thing
    }
  }
]

Output:


{
  "items" : [ 1, null, 2 ]
}

Expected:


{
  "items" : [ 1,  2 ]
}

The only way to resolve this is through shift operation which can get cumbersome if the array is nested in a deeper level and is part of complex json where you not only have to traverse the hierarchy but also accommodate all other fields\objects in the spec to maintain the same structure.

gbouget commented 4 months ago

A trick is to use a temporary parent node because recursivelySquashNulls only works on child nodes.

[
  {
    "operation": "shift",
    "spec": {
      "*": "tmpRoot.&"
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "tmpRoot": "=recursivelySquashNulls" //squashNulls same thing
    }
  },
  {
    "operation": "shift",
    "spec": {
      "tmpRoot": {
        "*": "&"
      }
    }
  }
]