Created 04-16-2023 01:03 AM
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.