json NifiJolt嵌套数组转换为自定义对象数组

xienkqul  于 2023-02-10  发布在  其他
关注(0)|答案(1)|浏览(140)

我绝对难住了尝试从JSON输入中获取嵌套数组,并使用Nifi Jolt Transform将它们转换为具有适当键和值的对象数组。
我遇到的问题是需要手动指定列名,而它们在JSON响应中并不明显。
列名为

{
  "column_names": [
    "icao24",
    "callsign",
    "origin_country",
    "time_position",
    "last_contact",
    "long",
    "lat",
    "baro_altitude",
    "on_ground",
    "velocity",
    "true_track",
    "vertical_rate",
    "sensors",
    "geo_altitude",
    "squawk",
    "spi",
    "position_source"
  ]
}

输入数据示例:

{
  "time": 1675791934,
  "states": [
    [
      "a57b26",
      "N452SM  ",
      "United States",
      1675791621,
      1675791621,
      -105.1168,
      39.9103,
      null,
      true,
      0,
      90,
      null,
      null,
      null,
      null,
      false,
      0
    ],
    [
      "aa56da",
      "UAL1986 ",
      "United States",
      1675791933,
      1675791933,
      -122.1349,
      41.1152,
      10972.8,
      false,
      235.33,
      2.51,
      0,
      null,
      11049,
      null,
      false,
      0
    ]
  ]
}

预期产出:

[
  {
    "icao24": "a57b26",
    "callsign": "N452SM  ",
    "origin_country": "United States",
    "time_position": 1675791621,
    "last_contact": 1675791621,
    "long": -105.1168,
    "lat": 39.9103,
    "baro_altitude": null,
    "on_ground": true,
    "velocity": 0,
    "true_track": 90,
    "vertical_rate": null,
    "sensors": null,
    "geo_altitude": null,
    "squawk": null,
    "spi": false,
    "position_source": 0
  },
  {...}
]

我从移动震动规范得出的结果是...

[
  {
    "operation": "shift",
    "spec": {
      "states": {
        "*": {
          "*": "[&1]"
        }
      }
    }
  }
]

结果:

[
  [
    "a57b26",
    "N452SM  ",
    "United States",
    1675791621,
    1675791621,
    -105.1168,
    39.9103,
    null,
    true,
    0,
    90,
    null,
    null,
    null,
    null,
    false,
    0
  ],
  [
    "aa56da",
    "UAL1986 ",
    "United States",
    1675791933,
    1675791933,
    -122.1349,
    41.1152,
    10972.8,
    false,
    235.33,
    2.51,
    0,
    null,
    11049,
    null,
    false,
    0
  ]
]

如果在有效负载中发送列名,我可能会弄清楚,但不幸的是,我需要在转换中手动设置它们。
我碰巧找到了一个类似情况的有用堆栈,但同样,解决方案看起来像重型正则表达式,我只是还没有很好地精通Nifi Jolt。
Jolt reference first element in array as target name

olmpazwi

olmpazwi1#

最后我自己解决了这个问题:

[
  {
    "operation": "default",
    "spec": {
      "temp": [
        [
          "icao24",
          "callsign",
          "origin_country",
          "time_position",
          "last_contact",
          "long",
          "lat",
          "baro_altitude",
          "on_ground",
          "velocity",
          "true_track",
          "vertical_rate",
          "sensors",
          "geo_altitude",
          "squawk",
          "spi",
          "position_source"
        ]
      ]
    }
  },
  {
    "operation": "shift",
    "spec": {
      "temp": {
        "*": "states[]"
      },
      "states": {
        "*": "states[]"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "states": {
        "*": {
          "*": "[&1].@(2,[0].[&])"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "0": null,
      "*": "[]"
    }
  }
]

这是预期输出:

[
  {
    "icao24": "a57b26",
    "callsign": "N452SM  ",
    "origin_country": "United States",
    "time_position": 1675791621,
    "last_contact": 1675791621,
    "long": -105.1168,
    "lat": 39.9103,
    "baro_altitude": null,
    "on_ground": true,
    "velocity": 0,
    "true_track": 90,
    "vertical_rate": null,
    "sensors": null,
    "geo_altitude": null,
    "squawk": null,
    "spi": false,
    "position_source": 0
  },
  {
    "icao24": "aa56da",
    "callsign": "UAL1986 ",
    "origin_country": "United States",
    "time_position": 1675791933,
    "last_contact": 1675791933,
    "long": -122.1349,
    "lat": 41.1152,
    "baro_altitude": 10972.8,
    "on_ground": false,
    "velocity": 235.33,
    "true_track": 2.51,
    "vertical_rate": 0,
    "sensors": null,
    "geo_altitude": 11049,
    "squawk": null,
    "spi": false,
    "position_source": 0
  }
]

相关问题