如何将 JSON 数组值与特定 JSON 结构输出进行比较和提取?

问题描述 投票:0回答:1

我有一个 JSON 对象,其中包含两个数组:idByLine 和 orderLine。我需要从 idByLine 中提取值,将它们与 orderLine 中的值进行比较,然后使用 NiFi 中的 Jolt Transform 生成具有特定结构的 JSON 输出。这是我正在使用的 JSON 结构:

{
  "orderLine": [
    {
      "ID": "1",
      "Country": "Country1",
      "Email": [
        {
          "email1": "email1"
        },
        {
          "email2": "email2"
        }
      ]
    },
    {
      "ID": "2",
      "Country": "Country2",
      "Email": [
        {
          "email1": "email1"
        },
        {
          "email2": "email2"
        }
      ]
    }
  ],
  "idByLine": [
    {
      "idLine": "1",
      "orderId": "AMD123"
    },
    {
      "idLine": "2",
      "orderId": "AMD555"
    }
  ]
}

我需要确保 idByLine 的 idLine 值存在于输出 json 中,而其他字段则保留在输出 json 示例中。我如何使用 nifi apache 的 JoltTransfor 来实现这一点?

我需要的输出JSON结构是:

[
 {
    "idLine": "AMD123",
    "ID": "1",
    "Country": "Country1",
    "Email": [
      {
        "email1": "email1"
      },
      {
        "email2": "email2"
      }
    ]
 },
 {
    "idLine": "AMD555",
    "ID": "2",
    "Country": "Country2",
    "Email": [
      {
        "email1": "email1"
      },
      {
        "email2": "email2"
      }
    ]
 }
]
json apache-nifi jolt
1个回答
0
投票

您可以使用以下转换:

[
  {
    "operation": "shift",
    "spec": {
      "idByLine": {
        "*": {
          "orderId": "[&1].idLine"
        }
      },
      "orderLine": {
        "*": {
          "ID": "[&1].ID",
          "Country": "[&1].Country",
          "Email": "[&1].Email"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": ""
    }
  }
]

© www.soinside.com 2019 - 2024. All rights reserved.