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):
{
"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:
[
{
"operation": "shift",
"spec": {
"modules": {
"*": {
"id": "new_modules[&1].id"
}
}
}
}
]
The output I get is:
{
"new_modules": [
{
"id": "1"
},
{
"id": "2"
}
]
}
What should I do to get all modules in the target JSON:
{
"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.
>Solution :
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.