Our Community is getting an upgrade! To get everything ready for the relaunch, we’ll be placing the site in read-only mode starting September 21st.
We really appreciate your understanding while we get things set up behind the scenes. Catch up on all the exciting details about the move here.
Need help or have questions? Drop us a line at [email protected]
Created on
04-16-2023
01:03 AM
- last edited on
04-21-2026
12:16 AM
by
GrazittiAPI
Hello,
I am using jolt to change the structure of a payload to another form, and it must be dynamic, especially the top level elements "test" which can be multiple. I have tried but not getting it. I would appreciate your kind help.
Here is my input json:
{
"test1": "test output",
"test2": "test output",
"Details": {
"0": {
"id": "first",
"name": "the first one"
},
"1": {
"id": "second",
"name": "the second one"
}
}
}
and my output json is :
{
"Details" : [ {
"test1" : "test output",
"test2" : "test output",
"id" : "first",
"name" : "the first one"
}, {
"test1" : "test output",
"test2" : "test output",
"id" : "second",
"name" : "the second one"
} ]
}
my current jolt spec is:
[
{
"operation": "shift",
"spec": {
"Details": "Details",
"*": "outer.&"
}
},
{
"operation": "shift",
"spec": {
"Details": {
"*": {
"*": "Details[&1].&",
"@(2,outer)": "Details[&1].outer"
}
}
}
},
{
"operation": "shift",
"spec": {
"Details": {
"*": {
"outer": {
"*": "Details[&2].&"
},
"*": "Details[&1].&"
}
}
}
}
]
But I am not getting the desired output json as above.
Created 04-17-2023 09:21 AM
Try the below spec. Again not sure if this is the most efficient way. You might need to rethink your strategy if you are dealing with a lot of data:
[
{
//package all test fields into an outer object
"operation": "shift",
"spec": {
"test*": "outer.&",
"Details": "Details"
}
},
{
// Insert each outer object into Details element 0,1,..
"operation": "shift",
"spec": {
"Details": {
"*": {
"*": "Details.&1.&",
"@(3,outer)": "Details.&1.outer"
}
}
}
},
{
//Bucket each outer object (test1, test2...) into each details element
"operation": "shift",
"spec": {
"Details": {
"*": {
"*": "Details.&1.&",
"outer": {
"*": {
"@": "Details.&3.&"
}
}
}
}
}
},
{
//package details into seperate element under an Array
"operation": "shift",
"spec": {
"Details": {
"*": "[#1].Details.&"
}
}
}
]
Created 04-16-2023 08:19 AM
Hi,
You have not specified the desired output, but would the following give you what you are looking for:
[
{
"operation": "shift",
"spec": {
"Details": {
"*": {
"*": "Details[#2].&"
}
},
"*": "outer[].&"
}
},
{
"operation": "shift",
"spec": {
"Details": "Details",
"outer": {
"*": {
"*": "Details[&1].&"
}
}
}
}
]if that helps please accept solution.
Thanks
Created on 04-16-2023 08:38 AM - edited 04-16-2023 08:51 AM
Thank you sir @SAMSAL .
Actually the desired output is
{
"Details" : {
"0":{
"test1" : "test output",
"test2" : "test output",
"id" : "first",
"name" : "the first one"
}},
"Details":{
"1":{
"test1" : "test output",
"test2" : "test output",
"id" : "second",
"name" : "the second one"
}}
}
Created on 04-16-2023 11:49 AM - edited 04-16-2023 11:50 AM
Im not sure what you specified is makes sense because you would have two keys with the same name "Details" but different values. I assume what you want is this:
{
"Details": {
"0": {
"test1": "test output",
"test2": "test output",
"id": "first",
"name": "the first one"
},
"1": {
"test1": "test output",
"test2": "test output",
"id": "second",
"name": "the second one"
}
}
}
In this case the spec would be like this:
[
{
//package all test fields into an outer object
"operation": "shift",
"spec": {
"test*": "outer.&",
"Details": "Details"
}
},
{
// Insert each outer object into Details element 0,1,..
"operation": "shift",
"spec": {
"Details": {
"*": {
"*": "Details.&1.&",
"@(3,outer)": "Details.&1.outer"
}
}
}
},
{
//Bucket each outer object (test1, test2...) into each details element
"operation": "shift",
"spec": {
"Details": {
"*": {
"*": "Details.&1.&",
"outer": {
"*": {
"@": "Details.&3.&"
}
}
}
}
}
}
]
Not sure if this is the best way, if someone knows a better way please provide your suggestion.
If that answers your question please accept solution.
Thanks
Created 04-16-2023 12:58 PM
I am very grateful for your help so far sir @SAMSAL.
Actually, i made a silly mistake concerning that key duplication.
I wanted to have something of this nature, that is separate objects each with "Details" topmost level.
"Details" : {
"0":{
"test1" : "test output",
"test2" : "test output",
"id" : "first",
"name" : "the first one"
}},
"Details":{
"1":{
"test1" : "test output",
"test2" : "test output",
"id" : "second",
"name" : "the second one"
}}
Created 04-17-2023 09:21 AM
Try the below spec. Again not sure if this is the most efficient way. You might need to rethink your strategy if you are dealing with a lot of data:
[
{
//package all test fields into an outer object
"operation": "shift",
"spec": {
"test*": "outer.&",
"Details": "Details"
}
},
{
// Insert each outer object into Details element 0,1,..
"operation": "shift",
"spec": {
"Details": {
"*": {
"*": "Details.&1.&",
"@(3,outer)": "Details.&1.outer"
}
}
}
},
{
//Bucket each outer object (test1, test2...) into each details element
"operation": "shift",
"spec": {
"Details": {
"*": {
"*": "Details.&1.&",
"outer": {
"*": {
"@": "Details.&3.&"
}
}
}
}
}
},
{
//package details into seperate element under an Array
"operation": "shift",
"spec": {
"Details": {
"*": "[#1].Details.&"
}
}
}
]
Created 04-19-2023 03:16 AM
Thank you sir @SAMSAL .
I am very grateful, at the moment, your solution does the job.
Also, i would love to learn JoltTransformation from you.
Thanks once again.