简体   繁体   English

使用Apache Nifi将JSON行转换为JSON数组

[英]Convert JSON Lines to JSON array using Apache Nifi

I have a file that includes (schemaless) JSON Lines encoded data. 我有一个包含(无模式)JSON行编码数据的文件。

For example: 例如:

{"foo" : "abc", "bar" : "def" }
{"foo" : "xyz" }
{"foo" : "ghi", "bar" : "jkl", "name" : "The Dude"}

I would like to use NIFI to convert this into a JSON array: 我想使用NIFI将其转换为JSON数组:

[{"foo" : "abc", "bar" : "def" },{"foo" : "xyz" },{"foo" : "ghi", "bar" : "jkl", "name" : "The Dude"}]

The easiest way to accomplish this in Apache NiFi is to use two ReplaceText processors. 在Apache NiFi中完成此操作的最简单方法是使用两个ReplaceText处理器。 In the first, configure as: 在第一个中,配置为:

  • Search Value: \\}\\s*\\{ 搜索值: \\}\\s*\\{
  • Replacement Value: \\},\\{ 替换值: \\},\\{
  • Replacement Strategy: Regex Replace 替换策略:正则Regex Replace
  • Evaluation Mode: Entire Text 评估模式: Entire Text

This will remove the line breaks between the tuples and insert commas between them. 这将删除元组之间的换行符,并在它们之间插入逗号。 In the second: 在第二:

  • Search Value: (^.*$) 搜索值: (^.*$)
  • Replacement Value: [$1] 重置价值: [$1]
  • Replacement Strategy: Regex Replace 替换策略:正则Regex Replace
  • Evaluation Mode: Entire Text 评估模式: Entire Text

This will add the enclosing brackets around the JSON array. 这将在JSON数组周围添加括号。 There are other ways to accomplish this with ExecuteScript or JoltTransformJSON processors, but they are more complicated and brittle. 还有其他方法可以使用ExecuteScriptJoltTransformJSON处理器来完成此JoltTransformJSON ,但是它们更加复杂且脆弱。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM