nifi 中的 JSON 数组到多个 JSON 对象 [英] Array of JSON to Mupliple JSON Object in nifi
问题描述
我想在 Nifi 中实现以下请求-响应场景.我的目标是在更多不同的处理器中使用每个值作为数组(对象键 1,对象键 2,...).
因此,如果我可以将其转换为多个 JSON,那么使用拆分 JSON 之后我可以使用多个值.
请为此提出各种解决方案.
输入 JSON:
<预><代码>[{"ID": "789654","日期": "2 月 29 日",键":[对象键 1"、对象键 2"、对象键 3"....]}]输出 JSON:
<预><代码> [{"ID": "789654","日期": "2 月 29 日",Key1":对象键 1"},{"ID": "789654","日期": "2 月 29 日","Key2" : "对象键 2"},{"ID": "789654","日期": "2 月 29 日","Key3" : "对象键 3"},......]你有两层数组.我假设在根数组中你可以有几个对象
<预><代码>[{"ID" : "111",日期":2 月 29 日","键" : ["对象键 1"、"对象键 2"、"对象键 3"、"对象键 4"]},{"ID" : "222",日期":2 月 27 日","Key" : ["对象键 5", "对象键 6"]}]使用以下流程
- SplitJson - 通过根数组分割文件
$
- EvaluateJsonPath - 将
$.ID
和$.Date
值提取到具有相应名称的属性中 - SplitJson - 通过
$.Key
拆分文件 - ReplaceText - 上一步的结果是无效的 json,因为
Key
中有一个字符串数组.你必须用双引号将字符串包裹在内容中:(?s)(^.*$)
->"$1"
- EvaluateJsonPath - 从上下文
$
中提取字符串到Key
属性 - AttributesToJson - 从属性构建 json 的最后一步
非常另类的变体
在脚本中使用 ExecuteGroovyScript
:
@Grab(group='acme.groovy', module='acmenifi', version='20190218')导入静态 groovyx.acme.nifi.AcmeNiFi.*withFlowFile(this).withJson{json,attr->json.each{o1->o1.Key.each{k1->//用json构建新文件newFlowFile(this).withJson{json2,attr2->attr2.putAll(attr)return o1 + [Key:k1]//设置新流文件的内容}}}return null//删除当前文件}
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.
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.
Input JSON:
[
{
"ID": "789654",
"Date": "29th Feb",
"Key" : ["object key 1", "object key 2", "object key 3"....]
}
]
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"]
}
]
use the following flow
- 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
$
intoKey
attribute - AttributesToJson - the last step to build json from attributes
extremely alternative variant
Use ExecuteGroovyScript
with script:
@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
}
这篇关于nifi 中的 JSON 数组到多个 JSON 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!