【问题标题】:How to write JOLT Spec for nested arrays?如何为嵌套数组编写 JOLT Spec?
【发布时间】:2023-04-10 03:44:01
【问题描述】:

我正在尝试使用 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 ]
} ]

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

【问题讨论】:

    标签: multidimensional-array apache-nifi specifications jolt


    【解决方案1】:

    您正在使用带有&4&5 的顶级数组索引,请尝试使用&2&3rows 索引):

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

    【讨论】:

    • 这是有效的。但我不明白您如何确定要指定的级别。
    • 索引从 0 开始,您从当前的“树中的级别”开始。所以从deviceId(索引0)你去“备份树”到value(索引1),然后到rows数组中的条目(索引2)。
    猜你喜欢
    • 2022-12-22
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多