具有颠簸变换的嵌套数组

时间:2019-11-21 17:21:27

标签: json apache-nifi jolt

我正在尝试将包含数组的JSON对象转换为将数据嵌套的JSON对象。基本上,我需要为数组的每个元素创建一个对象,并复制所有外部数据。示例:

输入

{
  "id": 3435,
  "startTime": "2019-10-30T10:00:00Z",
  "sportType": "FOOTBALL",
  "participants": [
    {
      "id": 1234,
      "name": "Team A",
      "position": "HOME"
    },
    {
      "id": 5678,
      "name": "Team B",
      "position": "AWAY"
    }
  ]
}

预期产量

{
  "id": 3435,
  "startTime": "2019-10-30T10:00:00Z",
  "sportType": "FOOTBALL",
  "participant_id": 1234,
  "participant_name": "Team A"
  "participant_position": "HOME"
},
{
  "id": 3435,
  "startTime": "2019-10-30T10:00:00Z",
  "sportType": "FOOTBALL",
  "participant_id": 5678,
  "participant_name": "Team B"
  "participant_position": "AWAY"
}  

我尝试过的SPEC:

[
  {
    "operation": "shift",
    "spec": {
      "id": "newID",
      "startTime": "startTime",
      "sportType": "sportType",
      "participants": {
        "*": {
          "id": "participant.&1.participant_id",
          "name": "participant.&1.participant_name",
          "position": "participant.&1.participant_position"
        }
      }
    }
  }
]

我对Jolt感到很陌生,并尝试了一些SPECS,但没有成功。我在嵌套部分遇到了麻烦。谢谢您的帮助。

1 个答案:

答案 0 :(得分:1)

此规范应该可以工作:

[
  {
    "operation": "shift",
    "spec": {
      "participants": {
        "*": {
          "id": "[#2].participant_id",
          "name": "[#2].participant_name",
          "position": "[#2].participant_position",
          "@(2,id)": "[#2].id",
          "@(2,startTime)": "[#2].startTime",
          "@(2,sportType)": "[#2].sportType"
        }
      }
    }
  }
]

为完整起见,ForkRecord和FlattenJson所做的事情与上述规范相似,使用起来可能比尝试找出JOLT规范容易。