Home > Software engineering >  Encapsulate a JSON Array inside an object with JOLT?
Encapsulate a JSON Array inside an object with JOLT?

Time:05-28

I work on a project where the output of one of our APIs is a JSON array. I'd like to encapsulate this array inside an object.

I try to use a JOLT transformation (this is the first time I use this tool) to achieve this. I've already searched through a lot of example, but I still can't figure out what my JOLT specification has to be to perform the transformation. I can't find what I am looking for.

For example, if my input is like this:

[
  {
    "id": 1,
    "name": "foo"
  },
  {
    "id": 2,
    "name": "bar"
  }
]

I'd like the output to be:

{
  "list":
  [
    {
      "id": 1,
      "name": "foo"
    },
    {
      "id": 2,
      "name": "bar"
    }
  ]
}

In short, I just want to put my array inside a field of another object.

CodePudding user response:

You can use a shift transformation spec such as

[
  {
    "operation": "shift",
    "spec": {
      "*": "list[]"
    }
  }
]

where "*" wildcard represents indices of the current wrapper array of objects

the demo on the site enter image description here

  • Related