Nifi JOLT:平面JSON对象到JSON对象列表



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

Thanks in advance

在阅读完这个问题后,JOLT转换沿着数组复制单个值

和答案https://stackoverflow.com/a/50438480/2733184

我能看出你们想要的东西出奇地相似。然而,我从来没有击中钉子的头需要问的问题。

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

[
{
"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
}
}
}
}
]

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

你可以遍历属性_区分的字符timestamp在单个shift内转换规范,如

[
{
"operation": "shift",
"spec": {
"*_*": {
"@1,timestamp": "values[#2].timestamp",
"$": "values[#2].key",
"@": "values[#2].value"
}
}
}
]

,

  • "@1,timestamp"表示在树向上移动一层后获取时间戳属性的值
  • by[#2]在右边,逻辑上意味着遍历:and{2当[# ]将生成数组结果

最新更新