JSON到Nifi中的多个JSON对象的数组 [英] Array of JSON to Mupliple JSON Object in nifi

查看:729
本文介绍了JSON到Nifi中的多个JSON对象的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Nifi中实现以下请求-响应方案.我的目标是在其他不同的处理器中将每个值用作数组(对象键1,对象键2 ...).

I want to achieve the below request-response scenario in Nifi. My objective is to use each value coming as an array(object key 1,object key 2,...) in further differt processors.

因此,如果我可以将其转换为多个JSON,则可以使用拆分JSON在以后使用多个值.

So if I can convert it into multiple JSON, then using split JSON I can use multiple vales later.

请为此提出各种解决方案.

Please suggest all sorts of solution for this.

输入JSON:

[
    { 
         "ID": "789654",
         "Date": "29th Feb",
         "Key" : ["object key 1", "object key 2", "object key 3"....]
    }
]

输出JSON:

Output JSON:

        [
            { 

             "ID": "789654",

            "Date": "29th Feb",

            "Key1" : "object key 1"

           },

           { 

             "ID": "789654",

            "Date": "29th Feb",

            "Key2" : "object key 2"

           },

          { 

             "ID": "789654",

            "Date": "29th Feb",

            "Key3" : "object key 3"

           },
           .
           .
           .
           .
           .
           .

         ]

推荐答案

您有两个级别的数组.我认为在根数组中您可能有几个对象

you have two levels array. i assume in the root array you could have several objects

[
    {
        "ID" : "111",
        "Date" : "29th Feb",
        "Key" : ["object key 1", "object key 2", "object key 3", "object key 4"]
    },
    {
        "ID" : "222",
        "Date" : "27th Feb",
        "Key" : ["object key 5", "object key 6"]
    }
]

使用以下流程

  • SplitJson-通过根数组$
  • 拆分文件
  • EvaluateJsonPath-将$.ID$.Date值提取到具有相应名称的属性中
  • SplitJson-通过$.Key
  • 分割文件
  • ReplaceText-上一步的结果是无效的json,因为您在Key中具有字符串数组.您必须用双引号将内容包装在字符串中:(?s)(^.*$)-> "$1"
  • EvaluateJsonPath-将上下文$中的字符串提取到Key属性中
  • AttributesToJson-从属性构建json的最后一步
  • SplitJson - split file by a root array $
  • EvaluateJsonPath - extract $.ID and $.Date values into attributes with corresponding names
  • SplitJson - split files by $.Key
  • ReplaceText - the result of previous step is non-valid json because you have an array of strings in Key. you have to wrap string in content with doublequotes: (?s)(^.*$) -> "$1"
  • EvaluateJsonPath - extract string from context $ into Key attribute
  • AttributesToJson - the last step to build json from attributes

极端替代变体

在脚本中使用ExecuteGroovyScript

@Grab(group='acme.groovy', module='acmenifi', version='20190218')
import static groovyx.acme.nifi.AcmeNiFi.*

withFlowFile(this).withJson{json,attr->
    json.each{o1->
        o1.Key.each{k1-> 
            //build new file with json
            newFlowFile(this).withJson{json2,attr2->
                attr2.putAll(attr)
                return o1 + [Key:k1] //set content of new flow file
            }
        }
    }
    return null //drop current file
}

这篇关于JSON到Nifi中的多个JSON对象的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆