Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jolt remove all null values

I want to achieve the JSON transformation using the Jolt processor. I have fields with null in my JSON and I would like to remove all these fields...

{
  "myValue": 345,
  "colorValue": null,
  "degreeDayValue": null,
  "depthValue": null,
  "distanceValue": null
}

...to just keep the myValue field.

Can I realize this with a Jolt operation removed?

like image 602
PhilG Avatar asked Jul 24 '26 18:07

PhilG


2 Answers

To remove null values from the internal array use the below spec at the end of your code:

{
  "operation": "modify-overwrite-beta",
  "spec": {
    "*": "=recursivelySquashNulls"
  }
}
like image 62
Anshu Solanki Avatar answered Jul 28 '26 16:07

Anshu Solanki


It is possible, just not straightforward. Requires two steps.

Spec::

[
  {
    "operation": "default",
    "spec": {
      // for all keys that have a null value
      //  replace that null value with a placeholder
      "*": "PANTS"
    }
  },
  {
    "operation": "shift",
    "spec": {
      // match all keys
      "*": {
        // if the value of say "colorValue" is PANTS
        //  then, match but do nothing.
        "PANTS": null,
        // otherwise, any other values are ok
        "*": {
          // "recreate" the key and the non-PANTS value
          // Write the value from 2 levels up the tree the "@1"
          //  to the key from 3 levels up the tree => "&2".
          "@1": "&2"
        }
      }
    }
  }
]

Produces:

{
  "myValue": 345
}
like image 29
Milo S Avatar answered Jul 28 '26 15:07

Milo S



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!