Duc Nguyen
Duc Nguyen

Reputation: 17

Use Jolt Transformation to seperate an array value into mulitiple keys in JSON

I have a JSON input like this:

Input:

{
  "data": {
    "data": [
      {
        "indicator": "abc",
        "value": [
          "1",
          "2",
          "3",
          "4",
          "5"
        ]
      },
      {
        "indicator": "def",
        "value": [
          "6",
          "7",
          "8",
          "9",
          "10"
        ]
      }
    ]
  }
}

I want a JSON output like this:

Output:

[
  {
    "indicator": "abc",
    "value1": "1",
    "value2": "2",
    "value3": "3",
    "value4": "4",
    "value5": "5"
  },
  {
    "indicator": "def",
    "value1": "6",
    "value2": "7",
    "value3": "8",
    "value4": "9",
    "value5": "10"
  }
]

Does anyone know how to write a JOLT Transformation spec to get this output?

I have tried but I cannot figure out how to solve this.

I hope that someone could help me write one. Thank you very much.

Upvotes: 1

Views: 39

Answers (1)

mattyb
mattyb

Reputation: 12083

This one is close:

[
  {
    "operation": "shift",
    "spec": {
      "data": {
        "data": {
          "*": {
            "indicator": "[&1].indicator",
            "value": {
              "*": "[&2].value&"
            }
          }
        }
      }
    }
  }
]

but since arrays are zero-based, you get the following output:

[
  {
    "indicator": "abc",
    "value0": "1",
    "value1": "2",
    "value2": "3",
    "value3": "4",
    "value4": "5"
  },
  {
    "indicator": "def",
    "value0": "6",
    "value1": "7",
    "value2": "8",
    "value3": "9",
    "value4": "10"
  }
]

You might be able to use the math functions on the keys to increment them by 1 if the above is not a viable solution.

Upvotes: 1

Related Questions