Cloudsan Chen
Cloudsan Chen

Reputation: 329

Jolt, map to array

I have a JSON input like the following:

{
  "p1": 1,
  "p2": 2,
  "p3": 3,
  "create_time": 12345
}

I would like to have produce an output like:

{
  "result": [
    {
      "name": "p1",
      "value": 1,
      "create_time": 12345
    },
    {
      "name": "p2",
      "value": 2,
      "create_time": 12345
    },
    {
      "name": "p3",
      "value": 3,
      "create_time": 12345
    }
  ]
}

any guide to this? or I should try another approach other than Jolt

Upvotes: 1

Views: 690

Answers (1)

Barbaros Özhan
Barbaros Özhan

Reputation: 65105

You can apply shift transformation twice ; combine all elements dissipated into each key which starts with a letter p in the first step, and then get rid of seperate key names by combining each objects as an array element within the array named result in the last step such as

[
  {
    "operation": "shift",
    "spec": {
      "p*": {
        "$": "&.name",
        "@(1,&)": "&.value",
        "@(1,create_time)": "&.create_time"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": "result[]"
    }
  }
]

or even make it shorter as

[
  {
    "operation": "shift",
    "spec": {
      "p*": {
        "$": "result[#2].name",
        "@(1,&)": "result[#2].value",
        "@(1,create_time)": "result[#2].create_time"
      }
    }
  }
]

Upvotes: 2

Related Questions