Json 从 Kafka 消息中读取的路径

Json Path Read from a Kafka Message

我有一条如下所示的 kafka 消息,我在其中尝试从 json 路径读取数据。但是,我在从 json 路径读取某些属性时遇到了挑战。这是示例消息。

样本 1:

{
  "header": {
    "bu": "google",
    "id": "12345",
    "bum": "google",
    "originTimestamp": "2021-10-09T15:17:09.842+00:00",
    "batchSize": "0",
    "jobType": "Batch"
  },
  "payload": {
    "derivationdetails": {
      "Id": "6783jhvvh897u31y283y",
      "itemid": "1234567",
      "batchid": 107,
      "attributes": {
        "itemid": "1234567",
        "lineNbr": "1498",
        "cat": "5929",
        "Id": "6783jhvvh897u31y283y",
        "indicator": "false",
        "subcat": "3514"
      },
      "Exception": {
        "values": [
          {
            "type": "PICK",
            "value": "blocked",
            "Reason": [
              "RULE"
            ],
            "rules": [
              "439"
            ]
          }
        ],
        "rulesBagInfo": [
          {
            "Idtype": "XXXX",
            "uniqueid": "7889423rbhevfhjaufdyeuiryeukjbdafvjd",
            "rulesMatch": [
              "439"
            ]
          }
        ]
      }
    }
  }
}

示例 2:相同的消息,但在“有效负载”中看到不同之处

{
  "header": {
    "bu": "google",
    "id": "12345",
    "bum": "google",
    "originTimestamp": "2021-10-09T15:17:09.842+00:00",
    "batchSize": "0",
    "jobType": "Batch"
  },
  "payload": {
      "Id": "6783jhvvh897u31y283y",
      "itemid": "1234567",
      "batchid": 107,
      "attributes": {
        "itemid": "1234567",
        "lineNbr": "1498",
        "cat": "5929",
        "Id": "6783jhvvh897u31y283y",
        "indicator": "false",
        "subcat": "3514"
      },
      "Exception": {
        "values": [
          {
            "type": "PICK",
            "value": "blocked",
            "Reason": [
              "RULE"
            ],
            "rules": [
              "439"
            ]
          }
        ],
        "rulesBagInfo": [
          {
            "Idtype": "XXXX",
            "uniqueid": "7889423rbhevfhjaufdyeuiryeukjbdafvjd",
            "rulesMatch": [
              "439"
            ]
          }
        ]
      }
  }
}

如果您观察,有时消息有“推导细节”,有时则没有。但不管它是否存在,我都需要读取 id、itemid 和 batchid 的值。我尝试使用

$.payload[*].id
$.payload[*].itemid
$.payload[*].batchid

但是我看到 batchid 是 returning null,即使它在消息中有一个值,如果我使用上面的,“attributes”下的属性 return null。对于“属性”下的字段,我使用这个(示例):

$.payload.attributes.itemId

而且,完全不知道如何阅读下面的部分。

"Exception": {
        "values": [
          {
            "type": "PICK",
            "value": "blocked",
            "Reason": [
              "RULE"
            ],
            "rules": [
              "439"
            ]
          }
        ],
        "rulesBagInfo": [
          {
            "Idtype": "XXXX",
            "uniqueid": "7889423rbhevfhjaufdyeuiryeukjbdafvjd",
            "rulesMatch": [
              "439"
            ]

我是新手,需要一些关于如何正确读取属性的建议。任何帮助都会很大 appreciated.Thanks

使用..(递归下降,深度扫描。JSONPath 从 E4X 借用此语法。)获取值。但是,如果有多个具有相同键的条目嵌套在深处,它将 return 一个列表。

下面的 jsonpath 表达式将 return list 样本 1 和样本 2 各有一项

$.payload..attributes.Id
$.payload..attributes.itemid
$.payload..batchid
$.payload..Exception