【问题标题】:Nifi JOLT: flat JSON object to a list of JSON objectNifi JOLT:平面 JSON 对象到 JSON 对象列表
【发布时间】:2021-03-11 10:31:04
【问题描述】:

我正在尝试使用 Nifi JOLT 处理器将平面 JSON 对象转换为 JSON 对象列表,如下面的输出所示。参数"p_7_1_0""px_2_7_1_0""pv_7_1_1" 的名称或数字可能不同(例如,我可以有 { "timestamp": 1559347670, "pw_2_1_0": 1, "p_2_2_1_0": 1 } )

有人可以帮我看看颠簸规格吗?

输入 Json

{
  "timestamp": 1559347670,
  "p_7_1_0": 6,
  "px_2_7_1_0": 1,
  "pv_7_1_1": 1
}

预期输出 JSON

{
  "values": [
    {
      "key": "p_7_1_0",
      "value": 6,
      "timestamp": 1559347670
    },
    {
      "key": "px_2_7_1_0",
      "value": 1,
      "timestamp": 1559347670
    },
    {
      "key": "pv_7_1_1",
      "value": 1,
      "timestamp": 1559347670
    }
  ]
}

提前致谢

【问题讨论】:

    标签: apache-nifi jolt


    【解决方案1】:

    读完这个问题JOLT transformation to copy single value along an array

    以及答案 https://stackoverflow.com/a/50438480/2733184

    我可以看到你想要的东西非常相似。但是,我永远不会在需要问的问题上一针见血。

    我鼓励你去前面提到的 Q 和 A,阅读所有内容(包括规范中的 cmets)并给他们点赞。

    [
      {
        "operation": "shift",
        "spec": {
          "timestamp": "timestamp",
          // put everything but the timestamp in a member (the pivot)
          "*": "all.&"
        }
      },
      {
        "operation": "shift",
        "spec": {
          "all": {
            "*": {
              // grab the member key and put it in its final place
              "$": "values[#2].key",
              // grab the member value and put it in its final place
              "@": "values[#2].value",
              // Walk back two steps (* -> all -> root) and grab the timestamp
              "@(2,timestamp)": "values[#2].timestamp"
              // I wish I understood the logic behind "#2" but I don't
              // and I'll have to read on it further
            }
          }
        }
      }
    ]
    

    我希望有人能解释一下# 的用途。我的直接猜测是它就像&(成员名称),但它看起来像是成员位置(?)。

    【讨论】:

    • 太棒了!非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多