Jolt, map to array

Viewed 220

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

1 Answers

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.[]"
    }
}]
Related