Search code examples
jsontransformjolt

Combine JSON arrays based on common key using Jolt


My JSON object looks like following:

{
  "array1": [
    {
      "key1": "value1", // common key
      "key2": "value2",
      "key3": "value3"
    },
    {
      "key1": "value1", // common key
      "key2": "value2",
      "key3": "value3"
    }
  ],
  "includes": {
    "array2": [
      {
        "key4": "value1", // common key
        "key5": "value5"
      },
      {
        "key4": "value1", // common key
        "key5": "value5"
      }
    ]
  }
}

I need to have the output in following format -

[ 
  {
    "key1": "value1",
    "key2": "value2",
    "key3": "value3",
    "key5": "value5"  // this comes from joining with array 2 based on key1 & key4
  }, 
  {
    "key1": "value1",
    "key2": "value2",
    "key3": "value3",
    "key5": "value5"  // this comes from joining with array 2 based on key1 & key4
  }
]

Based on Extract properties from multiple JSON arrays using Jolt transformation, I have an answer to make above work if same keys exist in both arrays (for ex: array1>key1 & array2>key1). But, in this case, both arrays have different keys (array1>key1 & array2>key4) with same values of course.

Please suggest how to match the keys in this case, and get the desired output. Thank you!


Solution

  • You can use successive shift transformation specs such as

    [
      { // combine key-value pairs of the arrays
        "operation": "shift",
        "spec": {
          "*": {
            "*": {
              "*": {
                "*": "[&1].&"
              },
              "@": "[&1]"
            }
          }
        }
      },
      { // exchange key-value pairs, and take keys under common values which are pretended to be key names now 
        "operation": "shift",
        "spec": {
          "*": {
            "*": {
              "$": "&2.@(0)"
            }
          }
        }
      },
      { // pick the first component for the Right Hand Side if they're list of keys
        "operation": "cardinality",
        "spec": {
          "*": {
            "*": "ONE"
          }
        }
      },
      { // get rid of key names of the objects 
        "operation": "shift",
        "spec": {
          "*": ""
        }
      }
    ]
    

    the demo on the site http://jolt-demo.appspot.com/ is

    enter image description here