JSON to JSON transformation - Need to define a JOLT specification which will remove empty string, space element from a group array element.
Below example has two sets of array group records with " " empty string and a space.
Input example:
{
"C1-RECORD": {
"C1-ACTIONS": {
"C1-MESSAGES": [
{
"C1-MSG-CODE": "UPT",
"C1-MSG-DEFINITION": "Update task"
},
{
"C1-MSG-CODE": "DEL",
"C1-MSG-DEFINITION": "Delete task"
},
{
"C1-MSG-CODE": " ",
"C1-MSG-DEFINITION": " "
},
{
"C1-MSG-CODE": " ",
"C1-MSG-DEFINITION": " "
}
]
}
}
}
JOLT Spec:
[
{
"operation": "shift",
"spec": {
"C1-RECORD": {
"C1-ACTIONS": {
"C1-MESSAGES": {
"*": {
"C1-MSG-CODE": "actions.messages[#2].msgCode",
"C1-MSG-DEFINITION": "actions.messages[#2].msgDefinition"
}
}
}
}
}
}
]
Output:
{
"actions" : {
"messages" : [ {
"msgCode" : "UPT",
"msgDefinition" : "Update task"
}, {
"msgCode" : "DEL",
"msgDefinition" : "Delete task"
}, {
"msgCode" : " ",
"msgDefinition" : " "
}, {
"msgCode" : " ",
"msgDefinition" : " "
} ]
}
}
The output is return array records with " " empty string and a space.
Need a JOLT transformation specs to remove the msgCode and msgDefinition fields without any value and with empty string " " and space.
The expected output should be:
> {
> "actions": {
> "messages": [
> {
> "msgCode": "UPT",
> "msgDefinition": "Update task"
> },
> {
> "msgCode": "DEL",
> "msgDefinition": "Delete task"
> }
> ]
> }
> }
I tried the below JOLT spec:
[
{
"operation": "shift",
"spec": {
"C1-RECORD": {
"C1-ACTIONS": {
"C1-MESSAGES": {
"*": {
"C1-MSG-CODE": {
" ": null,
"*": {
"$": "actions.messages.msgCode"
}
},
"C1-MSG-DEFINITION": {
" ": null,
"*": {
"$": "actions.messages.msgDefinition"
}
}
}
}
}
}
}
}
]
but the output came out as below which is not the right format:
{
"actions" : {
"messages" : {
"msgCode" : [ "UPT", "DEL" ],
"msgDefinition" : [ "Update task", "Delete task" ]
}
}
}
Trying to search for solution but not a lot out there.
Appreciate any help.
Thanks
CodePudding user response:
You can use this spec:
[
{
"operation": "shift",
"spec": {
"*": {
"*": {
"*": {
"*": {
"C1-MSG-CODE": {
" ": null,
"*": {
"@1": "actions.messages[&3].msgCode"
}
},
"C1-MSG-DEFINITION": {
" ": null,
"*": {
"@1": "actions.messages[&3].msgDefinition"
}
}
}
}
}
}
}
}
]
CodePudding user response:
You can just prepend your attributes with [#4]
in order to distinguish the values at the level of the indexes of "C1-MESSAGES"
array by going up the tree 4 level throuh traversing one column and three opening curly braces as that #
wildcard stays on the right hand side of the expression such as
[
{
"operation": "shift",
"spec": {
"C1-RECORD": {
"C1-ACTIONS": {
"C1-MESSAGES": {
"*": {
"C1-MSG-CODE": {
" ": null,
"*": {
"$": "actions.messages[#4].msgCode"
}
},
"C1-MSG-DEFINITION": {
" ": null,
"*": {
"$": "actions.messages[#4].msgDefinition"
}
}
}
}
}
}
}
}
]