Home > Enterprise >  How do I write a jolt transform spec to iterate through a loop with multiple arrays?
How do I write a jolt transform spec to iterate through a loop with multiple arrays?

Time:09-09

I have the following

Input JSON :

{
  "a": [
    [
      "a1"
    ],
    [
      "a2"
    ]
  ],
  "b": [
    [
      "b1",
      "b2"
    ],
    [
      "b3",
      "b4"
    ]
  ],
  "c": [
    [
      "c1"
    ],
    [
      "c2"
    ]
  ]
}

I want to use jolt transform to output it like this:

Output JSON:

[
  {
    "a": "a1",
    "b": "b1",
    "c": "c1"
  },
  {
    "a": "a1",
    "b": "b2"
  },
  {
    "a": "a2",
    "b": "b3",
    "c": "c2"
  },
  {
    "a": "a2",
    "b": "b4"
  }
]

What's the best way to stroll through the shift operations? I tried multiple shift operations between the 3 arrays but could not get the desired output.

CodePudding user response:

You can use the following specs with @(2,a) vs. @(2,c[&]) identifiers set as the object labels in order to get the desired output such as

[
  {
    // partition by indexes of array "a"&"b" while strolling through the array "b"
    "operation": "shift",
    "spec": {
      "b": {
        "*": {
          "@(2,a)": {
            "*": "&.&1.a"
          },
          "@(2,c[&])": {
            "*": "&1.&.c"
          },
          "*": "&1.&.&2"
        }
      }
    }
  },
  {
    // get rid of object labels
    "operation": "shift",
    "spec": {
      "*": {
        "*": ""
      }
    }
  },
  {
    // get rid of square brackets wrapping up the values of the attributes "a"
    "operation": "cardinality",
    "spec": {
      "*": {
        "a": "ONE"
      }
    }
  },
  {
    // order the attributes by the key names
    "operation": "sort"
  }
]
  • Related