I am trying to convert below input json using jolt
{
"Notification": {
"product": {
"version": 1,
"concepts": {
"concept": [
{
"records": {
"record": [
{
"attributes": {
"attribute": [
{
"name": "IsListed",
"content": "N"
}
]
}
}
]
},
"cversion": 1,
"cname": "ListingInfo"
},
{
"records": {
"record": [
{
"attributes": {
"attribute": [
{
"name": "Code",
"content": "ABCD"
},
{
"name": "Value",
"content": "Vall"
}
]
}
}
]
},
"cversion": 1,
"cname": "Reference"
}
]
}
}
}
}
The output I expect is given below. If the input contains multiple attribute blocks for a concept, then the output should start with an array [{},{}]
otherwise it should start with {}
{
"version": 1,
"ListingInfo": {
"name": "IsListed",
"content": "N"
},
"Reference": [
{
"name": "Code",
"content": "ABCD"
},
{
"name": "Value",
"content": "Vall"
}
]
}
The spec that I am using is given below
[
{
"operation": "shift",
"spec": {
"Notification": {
"product": {
"version": "version",
"concepts": {
"concept": {
"*": {
"records": {
"record": {
"*": {
"attributes": {
"attribute": {
"*": {
"name": "@(7,cname).[&1].&",
"content": "@(7,cname).[&1].&"
}
}
}
}
}
}
}
}
}
}
}
}
}
]
The output of the above spec produces an array even if there is only one element
{
"version": 1,
"ListingInfo": [
{
"name": "IsListed",
"content": "N"
}
],
"Reference": [
{
"name": "Code",
"content": "ABCD"
},
{
"name": "Value",
"content": "Vall"
}
]
}
What change is required in my spec to correct the array issue ?
You can remove square brackets to convert the notation from .[&1].
to .&1.
in your case, then call looping within an extra shift transformation spec, where no need to use square brackets, the array will be spontaneously formed for multiple objects stated on the common node, such as
[
{
"operation": "shift",
"spec": {
"@Notification.product": {
"version": "&", // use replacement operator "&"
"@concepts.concept": {
"*": {
"@records.record": {
"*": {
"@attributes.attribute": {
"*": {
"*": "@5,cname.&1.&" // combine individual attributes within a single line
}
}
}
}
}
}
}
}
},
{
"operation": "shift",
"spec": {
"version": "&",
"*": {
"*": "&1"
}
}
}
]
the demo on the site http://jolt-demo.appspot.com/ is :