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

[
  {
    "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].&"
          }
        }
      }
    }
  }
]

Leave a Reply