Alexandre
Alexandre

Reputation: 58

JSON string array to object array using JOLT

For a student project I have to improve data quality. The first step is to request an API. Secondly, we have to edit the json structure.

This is the response from the API :

{
    "lists": [
        [
            0,
            451,
            "test",
            "953"
        ],
        [
            2,
            1010,
            "hello",
            "610"
        ]
    ]
}

Now using jolt I want to have a result like that :

{
  "lists": [
    {
      "id": 0,
      "clientId": 451,
      "name": "test",
      "custom_value": "953"
    },
    {
      "id": 2,
      "clientId": 1010,
      "name": "hello",
      "custom_value": "610"
    }
  ]
}

Currently, I can access to data values but I don't know how to separate it into array with objects.

My 'code' :

[
  {
    "operation": "shift",
    "spec": {
      "lists": {
        "*": {
          "*": {
            "*": {
              "$0": "lists"
            }
          }
        }
      }
    }
  }
]

Where I'm wrong and how can I edit the structure of the original array properly?

Upvotes: 3

Views: 3530

Answers (1)

Milo S
Milo S

Reputation: 4586

Spec

[
  {
    "operation": "shift",
    "spec": {
      "lists": {
        "*": { // lists array
          "0": "lists[&1].id",
          "1": "lists[&1].clientId",
          "2": "lists[&1].name",
          "3": "lists[&1].custom_value"
        }
      }
    }
  }
]

Upvotes: 7

Related Questions