I am trying to transform a JSON into another JSON using JOLT.
My source JSON has the following format (the number of levels of child modules is not known):
Input
{
"modules": [
{
"id": "1",
"modules": [
{
"id": "1.1",
"modules": []
},
{
"id": "1.2",
"modules": [
{
"id": "1.2.1",
"modules": []
}
]
}
]
},
{
"id": "2",
"modules": [
{
"id": "2.1",
"modules": []
}
]
}
]
}
My JOLT transformation spec looks like this:
Spec:
The output I get is:
[
{
"operation": "shift",
"spec": {
"modules": {
"*": {
"id": "new_modules[&1].id"
}
}
}
}
]
Desired Output:
{
"new_modules": [
{
"id": "1"
},
{
"id": "1.1"
},
{
"id": "1.2"
},
{
"id": "1.2.1"
},
{
"id": "2"
},
{
"id": "2.1"
}
]
}
I am adding some unrelated text below as StackOverflow complains that my question is mostly code and that I must add more details. Kindly let me know if I am missing details.
CodePudding user response:
You can use this spec:
[
{
"operation": "shift",
"spec": {
"modules": {
"*": {
"id": "new_modules[].&",
"modules": {
"*": {
"id": "new_modules[].&",
"modules": {
"*": {
"id": "new_modules[].&",
"modules": {
"id": "new_modules[].&"
}
}
}
}
}
}
}
}
}
]
You can't have infinite levels in the jolt. But you can increase your levels in the above code.
CodePudding user response:
You can use such a shift transformation spec
[
{
"operation": "shift",
"spec": {
"modules": {
"*": {
"id": "&2[&1].x&1.&",// &2 represents going two levels up and grab the key value "modules", [&1] represents going one level up the tree to reach the level of the indexes of the "module" array and generating values in arraywise manner
"mod*": {// even using "*" is enough instead of "mod*" as an else case if the style is the same as in the current sample for all JSON values
"*": {
"id": "&4[&3].y&1.&",
"mod*": {
"*": {
"id": "&6[&5].z&1.&"
}
}
}
}
}
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"*": {
"*": "new_&2[]" // &2 replicates the literal "modules"
}
}
}
}
]
considering a JSON value with a 3 level of deepness at most.