Search code examples
jsonjolt

Jolt remove nested dict


I have the following input json (simplified; the real one has more keys):

{
    "key1":{
        "null":null
    },
    "key2":123
}

and I'd like like to simplify the null dict into a simple null like this:

{
    "key1": null,
    "key2":123
}

My latest attempt is the following jolt:

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

But this results in:

{
  "key1": [
    {
      "null": null
    },
    null
  ],
  "key2": 123
}

because the second rule is matcing again key1 and appending again the orignal value to the same position.

Does anybody know how to do this? Thanks in advance


Solution

  • You might use the conditional "null" vs. otehrs("*") nested within the object such as

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "null": "&1",
            "*": {
              "@1": "&2" // bring the value from 2 upper level,
              // where @ matches the 1st, @1 matches the 2nd level's value
              // while &2 replicates the key from 2 upper level  
            } 
          }
        }
      }
    ]
    

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

    enter image description here