Hi guys, I'm working on some complex filter which is configured to work for two different JSONs, the difference is that one of them has one more node. Everything works fine except in one corner case and I have difficulties with keeping the JSONs structure.
Here is my case:
JSON 1:
{
"header": {
"something": "something"
},
"payload": {
"decisions": [
{
"sequenceId": "1",
"decision": "serviceToDelete",
"something": "something"
}
],
"elements": [
{
"serviceName": "serviceToDelete",
"something": "something"
}
]
},
"moreStuff": []
}
JSON 2:
{
"message": {
"header": {
"something": "something"
},
"payload": {
"decisions": [
{
"sequenceId": "1",
"decision": "serviceToDelete",
"something": "something"
}
],
"elements": [
{
"serviceName": "serviceToDelete",
"something": "something"
}
]
},
"someStuff": {
"otherStuff": "stuff"
}
},
"moreStuff": []
}
FILTER
[
{
"operation": "shift",
"spec": {
"*": "&",
"message": {
"*": "&1.&",
"payload": {
"*": "&",
"decisions": {
"*": {
"decision": {
"serviceToDelete": null,
"*": {
"@2": "&6.&4"
}
}
}
},
"elements": {
"*": {
"serviceName": {
"serviceToDelete": null,
"*": {
"@2": "&6.&4"
}
}
}
}
},
"workflow": {
"*\\].*": {
"serviceToDelete": null,
"*": {
"@1": "&3.&(2,1).&2"
}
}
}
}
}
},
{
"operation": "shift",
"spec": {
"*": "&",
"workflow": {
"*": {
"*ServiceId": {
"@1": "&3"
}
}
}
}
},
{
"operation": "shift",
"spec": {
"*": "&",
"w*": {
"*": {
"*": {
"@": "message.&3.&"
}
}
}
}
},
{
"operation": "shift",
"spec": {
"*": "&",
"message": {
"*": "&1.&",
"decisions": "&1.payload.decisions",
"elements": "&1.payload.elements"
}
}
},
{
"operation": "shift",
"spec": {
"*": "&",
"payload": {
"*": "&",
"decisions": {
"*": {
"decision": {
"serviceToDelete": null,
"*": {
"@2": "decisions"
}
}
}
},
"elements": {
"*": {
"serviceName": {
"serviceToDelete": null,
"*": {
"@2": "elements"
}
}
}
}
}
}
},
{
"operation": "shift",
"spec": {
"*": "&",
"decisions": "payload.decisions",
"elements": "payload.elements"
}
}
]
OUTPUT 1
{
"header" : {
"something" : "something"
},
"moreStuff" : [ ]
}
OUTPUT 2
{
"message" : {
"header" : {
"something" : "something"
},
"someStuff" : {
"otherStuff" : "stuff"
}
},
"moreStuff" : [ ]
}
As you see here after filter the this node is missing:
"payload": {
"decisions": [],
"elements": []
}
I want to keep the JSON structure. So, this node will be there and just be empty. Like this:
EXPECTED 1:
{
"header": {
"something": "something"
},
"payload": {
"decisions": [],
"elements": []
},
"moreStuff": []
}
EXPECTED 2:
{
"message": {
"header": {
"something": "something"
},
"payload": {
"decisions": [],
"elements": []
},
"someStuff": {
"otherStuff": "stuff"
}
},
"moreStuff": []
}
Keep the "payload" node empty also with "decisions" and "elements".
CodePudding user response:
What you need is to add a default transformation spec along with a new shift spec such as
{
"operation": "default",
"spec": {
"*": {
"payload": {
"decisions": [],
"elements": []
}
}
}
},
{
"operation": "shift",
"spec": {
"*": "&",
"header": {
"*": "&1.&",
"payload": "&"
}
}
}
to the bottom of the current spec, just before the closing square bracket.