I’ve been writing a jolt for json transformation and I’m halfway through there. Need to modify json object in array if it is present in array and need to create new if it is not present at all. The object can appear at any position in json.
{
"id": "id_1",
"targetId": "targetId42",
"externalId": "1extid",
"textArray": [
{
"name": "attribute1",
"value": "value1",
"status": null
},
{
"name": "attribute2",
"value": "value2",
"status": null
},
{
"name": "attribute3",
"value": "to be modified",
"status": null
}
]
"createdDate": "2020-09-10",
"description": "Value Pack",
"id": "20020",
"state": "Complete",
"requestedCompletionDate": "2022-09-13"
}
Expected output
{
"id": "id_1",
"targetId": "targetId42",
"externalId": "1extid",
"textArray": [
{
"name": "attribute1",
"value": "value1",
"status": null
},
{
"name": "attribute2",
"value": "value2",
"status": null
},
{
"name": "attribute3",
"value": "modified value",
"status": null
}
]
"createdDate": "2020-09-10",
"description": "Value Pack",
"id": "20020",
"state": "Complete",
"requestedCompletionDate": "2022-09-13"
}
And if the block attribute3 is not present we create it with value that can be passed through custom attributes.
Note – the attribute3 object can appear at any position in textArray array.
Tried following different jolt but none desired output.
My current approach is to remove the "attribute3" block completely and then add new "attribute3" block with updated value.
[
{
"operation": "shift",
"spec": {
"textArray": {
"*": {
"@": "@(1,name)"
}
}
}
},
{
"operation": "remove",
"spec": {
"attribute3": ""
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"textArray": {
"*": "=toList(@(1,&))"
}
}
}
]
and after removing this perform another jolt transform which adds the ‘attribute3’ object with updated value.
[
{
"operation": "default",
"spec": {
"temp": {
"name": "attribute3",
"value": "${updatedValue}",
"status": null
}
}
},
{
"operation": "shift",
"spec": {
"*": "&",
"textArray": "textArray",
"temp": "textArray",
"status": null
}
}
]
2
Answers
You can use the following shift transformation spec
the demo on the site https://jolt-demo.appspot.com/ is :
There’s probably a better way: