如何为嵌套数组编写JOLT Spec?

时间:2019-04-16 09:05:06

标签: multidimensional-array apache-nifi specifications jolt

我正在尝试使用JOLT转换JSON。该JSON由嵌套数组组成,无法获得正确的转换。

这是原始JSON。注意,将始终只有一个带有单个对象的“结果”数组。该对象将始终包含单个“行”数组。我想要rows数组中每个元素的字段。

{
  "results": [
    {
      "total_rows": 1390,
      "offset": 0,
      "rows": [
        {
          "id": "00407a53-5f45-11e9-8b9c-84cc507154b4",
          "key": "weather",
          "value": {
            "_id": "00407a53-5f45-11e9-8b9c-84cc507154b4",
            "_rev": "1-e996404ab9445c8ff753d45f61b5dc16",
            "deviceType": "home-iot",
            "deviceId": "12345",
            "eventType": "weather",
            "format": "json",
            "timestamp": "2019-04-15T06:09:17.311Z",
            "data": {
              "temperature": 44,
              "humidity": 75
            }
          }
        },
        {
          "id": "00901ed4-5f44-11e9-94a1-84cc507154b4",
          "key": "weather",
          "value": {
            "_id": "00901ed4-5f44-11e9-94a1-84cc507154b4",
            "_rev": "1-519c4edaeb15ed2ca102d4aabe4a0339",
            "deviceType": "home-iot",
            "deviceId": "12345",
            "eventType": "weather",
            "format": "json",
            "timestamp": "2019-04-15T06:02:08.337Z",
            "data": {
              "temperature": -7,
              "humidity": 49
            }
          }
        }
      ]
    }
  ]
}

这是我写的规范:

[
  {
    "operation": "shift",
    "spec": {
      "results": {
        "*": {
          "rows": {
            "*": {
              "value": {
                "deviceId": "[&4].deviceId",
                "deviceType": "[&4].deviceType",
                "eventType": "[&4].eventType",
                "timestamp": "[&4].timestamp",
                "data": {
                  "*": "[&5].&"
                }
              }
            }
          }
        }
      }
    }
  }
]

期望的JSON是:

[{
    "deviceId": "12345",
    "deviceType": "home-iot",
    "eventType": "weather",
    "timestamp": "2019-04-15T06:09:17.311Z",
    "temperature": 44,
    "humidity": 75
}, {
    "deviceId": "12345",
    "deviceType": "home-iot",
    "eventType": "weather",
    "timestamp": "2019-04-15T06:02:08.337Z",
    "temperature": -7,
    "humidity": 49
}]

但是,我的规格返回以下输出:

[ {
  "deviceId" : [ "12345", "12345" ],
  "deviceType" : [ "home-iot", "home-iot" ],
  "eventType" : [ "weather", "weather" ],
  "timestamp" : [ "2019-04-15T06:09:17.311Z", "2019-04-15T06:02:08.337Z" ],
  "temperature" : [ 44, -7 ],
  "humidity" : [ 75, 49 ]
} ]

级别可能有问题。但我无法弄清楚。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:2)

您正在将顶级数组索引与&4&5一起使用,请尝试使用&2&3rows索引):< / p>

[
  {
    "operation": "shift",
    "spec": {
      "results": {
        "*": {
          "rows": {
            "*": {
              "value": {
                "deviceId": "[&2].deviceId",
                "deviceType": "[&2].deviceType",
                "eventType": "[&2].eventType",
                "timestamp": "[&2].timestamp",
                "data": {
                  "*": "[&3].&"
                }
              }
            }
          }
        }
      }
    }
  }
]