blong blong - 1 month ago 29
JSON Question

Re-parent a JSON object using Jolt

I'd like to move a JSON object down one level using Jolt. Here's my input data :

{
"rating": {
"primary": {
"value": 3,
"max": 5
},
"quality": {
"value": 3,
"max": 7
}
}
}


I'm using the following spec file :

[
{
"operation": "shift",
"spec": {
"$": "NEWPARENT"
}
}
]


I'm currently producing this output :

{
"NEWPARENT" : "root"
}


However, my goal is to produce :

{
"NEWPARENT": {
"rating": {
"primary": {
"value": 3,
"max": 5
},
"quality": {
"value": 3,
"max": 7
}
}
}
}


Can anyone help me create the proper spec file?

Answer

Spec

[
  {
    "operation": "shift",
    "spec": {
      "@": "NEWPARENT"
    }
  }
]

FYI, the "root" you are seeing is an artifact of the way that Jolt deals with the fact that the top level JSON input can be either a List or a Map, and that you want to be able to Transform a List into a Map (and vice versa).

So, jolt "wraps" the input so that it is "always" transforming a Map that has a single key "root", with a value that is the input.