I am stuck in transformation of JSON in JSONata. Any help on this is appreciated.
Is there any library which makes this possible in Node JS apart from JSONata.
Moreover direct transformation of the data is possible or needs manipulation in JS?
my input json looks like
"species": "mammels",
"title": [
{
"locale": "en-GB",
"value": "GB Title"
},
{
"locale": "en-US",
"value": "US Title"
},
{
"locale": "en-RS",
"value": "RS Title"
}
],
"shortDescription": [
{
"locale": "en-GB",
"value": "GB shortDesc"
},
{
"locale": "en-US",
"value": "US shortDesc"
}
],
"longDescription": [
{
"locale": "en-GB",
"value": "GB longDesc"
}
],
"zoos": [
{
"location": "Englang",
"value": "National Zoo"
},
{
"location": "Ireland",
"value": "Nature home"
}
],
}
I need transformed output as follows.
"dataSet":[
{
"data":[
{
"key":"Territory",
"value":"SG"
},
{
"key":"studios",
"value":"National Zoo,Nature home"
}
],
"locale":"none"
},
{
"data":[
{
"key":"Title",
"value":"GB Title"
},
{
"key":"shortDescription",
"value":"GB shortDesc"
},
{
"key":"longDescription",
"value":"GB longDesc"
}
],
"locale":"en-GB"
},
{
"data":[
{
"key":"Title",
"value":"US Title"
},
{
"key":"shortDescription",
"value":"US shortDesc"
}
],
"locale":"en-US"
},
{
"data":[
{
"key":"Title",
"value":"RS Title"
}
],
"locale":"en-RS"
}
]
}
{
"dataSet":[{
"territory": {"territory":species} ~> $each(function($v, $k) {
{
"locale": "none",
"data": [{
"key":$k,
"value":$v}
]
}
}),
"title": $spread(title {
locale: [ $ ~> | $ | {"key":"Title"}, "locale" | ]
}).{
"locale": $replace($keys()[0],"-","_"),
"data": *
},
"shortDescription": $spread(shortDescription {
locale: [ $ ~> | $ | {"key":"Synopsis"}, "locale" | ]
}).{
"locale": $replace($keys()[0],"-","_"),
"data": *
},"longDescription": $spread(longDescription {
locale: [ $ ~> | $ | {"key":"Description"}, "locale" | ]
}).{
"locale": $replace($keys()[0],"-","_"),
"data": *
},
"studios": $spread(zoos {
locale: [ $ ~> | $ | {"key":"studios"}, "locale" | ]
}).{
"locale": $replace($keys()[0],"-","_"),
"data": *
}
}.[title,shortDescription,longDescription,studios,territory]{locale:data} ~> $each(function($v, $k) {
{
"data": [$v],
"locale": $k
}
})]
}
Try it out here. https://try.jsonata.org/-ZRuUcUwM