Suman
Suman

Reputation: 868

Manipulate list of json into list of json using JOLT

I have an input json list as below:

[
  {
    "key_id": "1111",
    "key_1": "value_1",
    "key_2": true,
    "key_3": {
      "key_3_1": "value_3_1",
      "key_3_2": "value_3_2"
    }
  },
  {
    "key_id": "2222",
    "key_1": "value_1",
    "key_2": true,
    "key_3": {
      "key_3_1": "value_3_1",
      "key_3_2": "value_3_2"
    }
  }
]

I need this list to be transformed as below:

[
  {
    "id": "1111",               // Extract from key_id
    "data": {                   // complete json goes here within 'data'
      "key_id": "1111",
      "key_1": "value_1",
      "key_2": true,
      "key_3": {
        "key_3_1": "value_3_1",
        "key_3_2": "value_3_2"
      }
    }
  },
  {
    "id": "2222",
    "data": {
      "key_id": "2222",
      "key_1": "value_1",
      "key_2": true,
      "key_3": {
        "key_3_1": "value_3_1",
        "key_3_2": "value_3_2"
      }
    }
  }
]

Each json inside list should be transformed into another json with id and data fields. data field should have entire json and id should be key_id.

Upvotes: 0

Views: 77

Answers (1)

mattyb
mattyb

Reputation: 12083

This spec should give you the expected output:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "key_id": "[&1].id",
        "@": "[&1].data"
      }
    }
  }
]

Upvotes: 1

Related Questions