bazaarvoice / jolt

JSON to JSON transformation library written in Java.
Apache License 2.0
1.56k stars 329 forks source link

NULL getting Created in JOLT #782

Open Ansul1326 opened 5 years ago

Ansul1326 commented 5 years ago

Hello, I was trying to convert below Json but NULL were getting created by JOLT. Any idea how can it be converted

Input : { "Name": [ { "FirstName": "Richard", "Score": "100", "Subject": "Physics", "Grade": "8" }, { "FirstName": "John", "Grade": "8" }, { "FirstName": "Charlie", "Score": "50", "Subject": "Maths", "Grade": "8" }, { "FirstName": "Robert", "Grade": "8" } ] }

Desired Output :

{ "Name": [ { "FirstName": "Richard", "Grade": "8" }, { "FirstName": "John", "Grade": "8" }, { "FirstName": "Charlie", "Grade": "8" }, { "FirstName": "Robert", "Grade": "8" } ], "Marks": [ { "Score": "100", "Subject": "Physics" }, { "Score": "50", "Subject": "Maths" } ] }

JOLT I created:

[{ "operation": "shift", "spec": { "Name": { "*": { "FirstName": "Name[&1].FirstName", "Score": "Marks[&1].Score", "Subject": "Marks[&1].Subject", "Grade": "Name[&1].Grade" } } } } ]

niernier007 commented 5 years ago

This spec below can solve your problem:

[ { "operation": "shift", "spec": { "Name": { "": { "FirstName": "t.Name.[&1].FirstName", "Grade": "t.Name.[&1].Grade", "Subject": "t.Marks[&1].Subject", "Score": "t.Marks[&1].Score" } } } }, { "operation": "modify-overwrite-beta", "spec": { "": "=recursivelySquashNulls" } }, { "operation": "shift", "spec": { "t": "" } } ]

Ansul1326 commented 5 years ago

Hello niernier007, Thanks for replying. I tried the JOLT you shared but it was giving me the error Error running the Transform.

JOLT Chainr encountered an exception constructing Transform className:com.bazaarvoice.jolt.Modifier.Overwritr at index:1.

Did it work for you ?

wisthy commented 5 years ago

Hello, while pasting code in there, from time to time, I see * disapearing. Using ``` arround your code should avoid that issue and make it a lot more readable.

[
  {
    "operation": "shift",
    "spec": {
      "Name": {
        "*": {
          "FirstName": "t.Name.[&1].FirstName",
          "Grade": "t.Name.[&1].Grade",
          "Subject": "t.Marks[&1].Subject",
          "Score": "t.Marks[&1].Score"
        }
      }
    }
},
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": "=recursivelySquashNulls"
    }
},
  {
    "operation": "shift",
    "spec": {
      "t": ""
    }
}
]