JSON to JSON Conversion using Jolt

Viewed 23

I am new to Jolt. I need help to generate required output. Below are Input and required output JSON. I have written a jolt but its not generating required out put JSON. In Input We have one items array and one Response element. The Response Elements attribute are not being populated in Output.

Input JSON

{
  "RegDate": "/Date(1651017600000)/",
  "Items": [
    {
      "LineItem": "1",
      "PoNumber": "1111111",
      "PoItem": "00001111"
    },
    {
      "LineItem": "2",
      "PoNumber": "2222222",
      "PoItem": "0000222222"
    },
    {
      "LineItem": "3",
      "PoNumber": "3333333",
      "PoItem": "00003333333"
    }
  ],
  "Response": {
    "GrNumber": "9999999999",
    "GrYear": "2022"
  }
}

Required Output

{
  "RegDate": "/Date(1651017600000)/",
  "Items": [
    {
      "LineItem": "1",
      "PoNumber": "1111111",
      "PoItem": "00001111",
      "GrNumber": "9999999999",
      "GrYear": "2022"
    },
    {
      "PoNumber": "2222222",
      "PoItem": "0000222222",
      "GrNumber": "9999999999",
      "GrYear": "2022"
    },
    {
      "PoNumber": "3333333",
      "PoItem": "00003333333",
      "GrNumber": "9999999999",
      "GrYear": "2022"
    }
  ]
}

Jolt Spec tried

[
  {
    "operation": "shift",
    "spec": {
      "RegDate": "RegDate",
      "Items": {
        "*": {
          "LineItem": "Items.[&1].LineItem",
          "PoNumber": "Items.[&1].PoNumber",
          "PoItem": "Items[&1].PoItem",
          "@GrNumber": "Items[&1].@GrNumber"
        }
      },
      "Response": {
        "GrNumber": "Items.GrNumber"
      }
    }
  }
]
1 Answers

What you need more is to go two levels up the tree in order to get out Items, and then pick the respective values under Response object by using Response.GrNumber and Response.GrYear such as

[
  {
    "operation": "shift",
    "spec": {
      "RegDate": "RegDate",
      "Items": {
        "*": {
          "LineItem": "&2[&1].&",
          "PoNumber": "&2[&1].&",
          "PoItem": "&2[&1].&",
          "@(2,Response.GrNumber)": "&2[&1].GrNumber",
          "@(2,Response.GrYear)": "&2[&1].GrYear"
        }
      }
    }
  }
]

while

  • substituting the qualifiers Items and Items. with &2,

  • the current node values of LineItem,PoNumber,PoItem with &

the demo on the site http://jolt-demo.appspot.com/ is

enter image description here

Related