Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Jolt Transformation: Un-nest object of form "key":{"value":"xyz"} to "key":"value"

I need to unwrap JSON objects in order to reduce the nesting of input like

[
  {
    "key1": {
      "value": "abc"
    },
    "key2": {
      "value": "xyz"
    }
  },
  {
    "key1": {
      "value": "123"
    },
    "key2": {
      "value": "456"
    }
  }
]

Instead it can just map straight to the value without the unnecessary object nesting.

Input JSON

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[
  {
    "typedValues": {
      "key1": {
        "value": "abc"
      },
      "key2": {
        "value": "xyz"
      }
    }
  },
  {
    "typedValues": {
      "key1": {
        "value": "123"
      },
      "key2": {
        "value": "456"
      }
    }
  }
]

My spec attempt

I was able to remove the "typedValues" wrapping but unable to achieve the key:value reduction.

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "typedValues": {
          "@": ""
        }
      }
    }
  }

]

Output from attempt

[
  {
    "key1": {
      "value": "abc"
    },
    "key2": {
      "value": "xyz"
    }
  },
  {
    "key1": {
      "value": "123"
    },
    "key2": {
      "value": "456"
    }
  }
]

Desired output

[
  {
    "key1": "abc",
    "key2": "xyz"
  },
  {
    "key1": "123",
    "key2": "456"
  }
]

>Solution :

You can use a shift transformaion such as

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "*": {
            "value": "&3.&1" // &3 : going 3 levels up the tree to grab the outermost indexes of the wrapper objects
          }                  // &1 : replicating key 1/2 after going 1 level up
        }
      }
    }
  },
  { // to get rid of the object keys
    "operation": "shift",
    "spec": {
      "*": ""
    }
  }
]

or shorly match @value with & wildcard which represents the current level replication of the key values

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": { //typedValues
          "*": {
            "@value": "[&3].&"
          }
        }
      }
    }
  }
]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading