如何将节点移动到数组中并键入Nifi中的给定节点?

时间:2019-02-22 17:55:29

标签: json apache-nifi jolt

我有JSON个有效载荷;

[
   {
      "Samples":{
         "Load":{
            "itemId":"bx",
            "timestamp":"2019-01-28T16:13:39.387640Z",
            "name":null
         },
         "Press":{
            "itemId":"by",
            "timestamp":"2019-01-28T16:13:39.387640Z",
            "name":null
         }
      }
   }
]

我想收到JSON,如下所示:

{
   "Samples":{
      "Items":[
         {
            "tag_name":"Load",
            "itemId":"bx",
            "timestamp":"2019-01-28T16:13:39.387640Z",
            "name":null
         },
         {
            "tag_name":"Press",
            "itemId":"by",
            "timestamp":"2019-01-28T16:13:39.387640Z",
            "name":null
         }
      ]
   }
}

我该怎么做?我可以使用JolTransformRecord吗?该记录适合实时流播吗?

1 个答案:

答案 0 :(得分:1)

使用来自GitHub的非常相似的问题:Could you please assist me ? Moving nodes up into an array, and the key into the nodes,您可以找到解决方案。该示例首先复制以排列整个对象,然后添加tag_name

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "Samples": {
          "*": { // keys: Load or Press
            // Left hand side "@" means grab the whole object 
            //  that was the right hand side of Load or Press.
            // Then send it to Samples.Items array.
            "@": "Samples.Items[#2]",
            "$": "Samples.Items[#2].tag_name"
          }
        }
      }
    }
  }
]