在JOLT转换中处理json数组和单个对象

qojgxg4l  于 2023-10-21  发布在  其他
关注(0)|答案(1)|浏览(124)

在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 first非空非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"
      }
    }
  ]
}

预期输出JSON first非空非null值

{
  "systemName": "R12"
}
lkaoscv7

lkaoscv71#

您可以使用以下转换

[
  {
    "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
    }
  }
]

相关问题