0

I need to convert the nested json to flat json by given type of input, if its flat json or nested json gives output as flat json respectively. I haven't seen any correct resource related to the this topic. I given the inputs and outputs below.

I am having some trouble with converting the nested JSON to flat JSON, and didn't get any closer as to what is mentioned problem. I need to transform a JSON structure by using a JOLT spec. I use https://jolt-demo.appspot.com to test the following below. Help me, how can i get the expected out, with this code

Input-1:

{
  "MessageType": "CREATION",
  "Number": "123",
  "Status": "created sucessfully",
  "StopSequence": "1",
  "Code": [
    {
      "CodeName": "ABC",
      "ShortDescription": "short description about ABC",
      "TimeImpact": 234,
      "Rank": 1
    },
    {
      "ReasonCodeName": "XYZ",
      "ShortDescription": "short description about ABC",
      "TimeImpact": 123,
      "Rank": 2
    }
  ]
}

Input-2:

{
    "MessageType": "UPDATE",
    "Number": "345",
    "PNumber": "P123",
    "Status": "updated sucessfully",
    "StopSequence": "2",
    "Id": 1234,
    "LNumber": "34565",
    "DeviceID": "7645235",
    "Timestamp": "2015-10-01T16:00:00-05:00",
    "Timezone": "US/New_York",
    "TimezoneShortName": "EST",
    "Unlocode": "XXXX",
}

Expected Output-1 if input 1 passes:

[
  {
    "MessageType": "CREATION",
    "Number": "123",
    "Status": "created sucessfully",
    "StopSequence": "1",
    "Code": "1",
    "CodeName": "ABC",
    "ShortDescription": "short description about ABC",
    "TimeImpact": 234,
    "CodeRank": 1
  },
  {
    "MessageType": "CREATION",
    "Number": "123",
    "Status": "created sucessfully",
    "StopSequence": "1",
    "Code": "2",
    "ReasonCodeName": "XYZ",
    "ShortDescription": "short description about ABC",
    "TimeImpact": 123,
    "Rank": 2
  }
]

Expected Output-2 if input 2 passes:

{
    "MessageType": "UPDATE",
    "PNumber": "P123",
    "Status": "updated sucessfully",
    "StopSequence": "2",
    "Id": 1234,
    "Timestamp": "2015-10-01T16:00:00-05:00",
    "Timezone": "US/New_York",
}

1 Answer 1

1

What you need is to repeat as much as the number of objects of the Code array. So, walk through by them as picking the related values from the out of the array such as

[
  {
    "operation": "shift",
    "spec": {
      "Code": {
        "*": {
          "@(2,MessageType)": "[&1].MessageType", // "@(2,MessageType)" means going up the tree 2 levels to reach the position of "MessageType" , [&1] means combining the values as array of objects depending on the indexes of the array 
          "@(2,Number)": "[&1].Number",
          "@(2,Status)": "[&1].Status",
          "@(2,StopSequence)": "[&1].StopSequence",
          "@(0,Rank)": "[&1].Code", // to copy the values of the "Rank" to "Code"
          "*": "[&1].&"
        }
      },
      "MessageT*|PNu*|St*|Id|Times*|Timezone": "&"
    }
  }
]
Sign up to request clarification or add additional context in comments.

2 Comments

Hi @Barbaros Özhan. Thank you for the response. I've added one case condition. Please lookin to. I need one jolt spec which will handle both the condition
@VamsiKumar_gudala I've added the desired condition as well

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.