在 JSON 输入中,我有时会得到数组,有时会得到 json 中没有数组标签的单个对象。如何使用 Jolt 转换来转换 JSON,期望两种场景作为输入
不带数组的 JSON :
{
"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"
}
}
}
预期 JSON 第一个非空非 null :
{
"systemName": "R12",
}
带有数组的 JSON :
{
"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"
}
}
]
}
预期输出 JSON 首先不为空非空值 :
{
"systemName": "R12"
}
您可以使用以下转换
[
{
"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
}
}
]