Home > database >  Jolt transform array of documents. Move parent attributes to a child
Jolt transform array of documents. Move parent attributes to a child

Time:08-30

I have an array of json objects that I need to transform using JOLT

Input

[
  {
    "name": "john",
    "age": 12
  },
  {
    "name": "tom",
    "age": 12
  }
]

Expected Output

[
  {
    "details": {
      "name": "john",
      "age": 12
    }
  },
  {
    "details": {
      "name": "tom",
      "age": 12
    }
  }
]

I currently have the following spec:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "@": "details.&"
      }
    }
  }
]

which results in the entire array of objects being wrapped in "details" as well as each element of the array being assigned it index as its key.

{
  "details" : {
    "0" : {
      "name" : "john",
      "Age" : 12
    },
    "1" : {
      "name" : "tom",
      "Age" : 12
    }
  }
}

CodePudding user response:

You can revert the order as "&.details" in order to distinguish the objects by uncommon values(1,2,...) rather than the common literal details such as

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "@": "&.details"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": ""
    }
  }
]

then, get rid of the object labels within the last transformation spec

the demo on the site enter image description here

  • Related