In JSON input I get some times array and some times single object without array tag in json. How to transform the JSON using Jolt transformation expecting both scenarios as a input
JSON without array :
{
"eventMetadata": {
"eventType": "Insert",
"baseObjectUid": "BASE_OBJECT",
"orsId": "orcl-TCR_HUB",
"triggerUid": "MESSAGE_QUEUE_RULE",
"messageId": "300894188",
"messageDate": "2023-08-30T23:54:32.335Z"
},
"insertEvent": {
"sourceSystemName": "R12",
"sourceKey": "XXXXX",
"eventDate": "2023-08-30T23:54:32.335Z",
"rowid": "9800029",
"xrefKey": {
"systemName": "R12",
"sourceKey": "XXXXX"
}
}
}
Expected JSON first non empty non null :
{
"systemName": "R12",
}
JSON with array :
{
"eventMetadata": {
"eventType": "Insert",
"baseObjectUid": "BASE_OBJECT",
"orsId": "orcl-TCR_HUB",
"triggerUid": "MESSAGE_QUEUE_RULE",
"messageId": "300894188",
"messageDate": "2023-08-30T23:54:32.335Z"
},
"insertEvent": [
{
"sourceSystemName": "R12",
"sourceKey": "XXXXX",
"eventDate": "2023-08-30T23:54:32.335Z",
"rowid": "9800029",
"xrefKey": {
"systemName": "",
"sourceKey": "XXXXX"
}
},
{
"sourceSystemName": "R12",
"sourceKey": "XXXXX",
"eventDate": "2023-08-30T23:54:32.335Z",
"rowid": "9800029",
"xrefKey": {
"systemName": "R12",
"sourceKey": "XXXXX"
}
},
{
"sourceSystemName": "R12",
"sourceKey": "XXXXX",
"eventDate": "2023-08-30T23:54:32.335Z",
"rowid": "9800029",
"xrefKey": {
"systemName": "Y12",
"sourceKey": "XXXXX"
}
}
]
}
Expected Output JSON first not empty non null value :
{
"systemName": "R12"
}
You can use the following transformation
[
{
"operation": "shift",
"spec": {
"@insertEvent": {
"*": { // --> the indexes are used for the array
"@xrefKey.systemName": { // the conditional for the array xrefKey + systemName attribute values starts here
"": "", // blank values
"*": { // values with length > 0
"$": "systemName[]" // current key values which are the values
// arraywisely( [] ) inherited from the upper node,
}
}
},
"@xrefKey.systemName": { // the conditional for the object xrefKey + systemName attribute values starts here
"": "", // blank values
"*": { // values with length > 0
"$": "systemName[]" // current key values which are the values
// arraywisely( [] ) inherited from the upper node,
}
}
}
}
},
{ // in order to pick the first component from the array
"operation": "modify-overwrite-beta",
"spec": {
"*": "=firstElement(@(1,&))" // derive the value of the array( & )
// from the current right-hand-side( 1 ) level
}
}
]