[英]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: 在第一个中,配置为:
\\}\\s*\\{
\\}\\s*\\{
\\},\\{
\\},\\{
Regex Replace
Regex Replace
Entire Text
Entire Text
This will remove the line breaks between the tuples and insert commas between them. 这将删除元组之间的换行符,并在它们之间插入逗号。 In the second:
在第二:
(^.*$)
(^.*$)
[$1]
[$1]
Regex Replace
Regex Replace
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. 还有其他方法可以使用
ExecuteScript
或JoltTransformJSON
处理器来完成此JoltTransformJSON
,但是它们更加复杂且脆弱。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.