简体   繁体   English

如何使用 NiFi 将字符串转换为 JSON 数组

[英]How to convert a string to a JSON array using NiFi

In NiFi I'm processing a flowfile containing the following attribute:在 NiFi 中,我正在处理一个包含以下属性的流文件:

Key: 'my_array'
    Value: '[u'firstElement', u'secondElement']'

I'd like to split flowFile on this array to process each element separately (and then merge).我想在这个数组上拆分 flowFile 以分别处理每个元素(然后合并)。 I tried to use SplitJson processor, but it requires JSON content to operate on, so I used AttributesToJSON before it.我尝试使用SplitJson处理器,但它需要 JSON 内容来操作,所以我在它之前使用了AttributesToJSON Unfortunately the produced flowFile's content is:不幸的是,产生的 flowFile 的内容是:

{"my_array": "[u'firstElement', u'secondElement'"}

And I receive the error我收到错误

The evaluated value [u'firstElement', u'secondElement'] of $['my_array'] was not a JSON Array compatible type and cannot be split. $['my_array'] 的计算值 [u'firstElement', u'secondElement'] 不是 JSON 数组兼容类型,无法拆分。

Is it possible to convert my_array string to the correct JSON array?是否可以将my_array字符串转换为正确的 JSON 数组? Do I need to use ExecuteScript or is there some simpler way?我需要使用ExecuteScript还是有一些更简单的方法?

How about ReplaceText with Replacement Strategy of Always Replace and Replacement Value of ${my_array} and then SplitJSON ?如何ReplaceTextReplacement StrategyAlways ReplaceReplacement Value${my_array} ,然后SplitJSON This will replace your FlowFile's content with this attribute's value and then you could SplitJSON on it.这将使用此属性的值替换 FlowFile 的内容,然后您可以在其上使用SplitJSON

Suppose I want to string : "Hashtags": "['tag1','tag2']" (a s part of my resultant json in Nifi, ) to be changed into : "Hashtags": ['tag1','tag2'].假设我想将字符串 : "Hashtags": "['tag1','tag2']" (作为Nifi 中生成的 json 的一部分) 更改为: "Hashtags": ['tag1','tag2' ]。

what I do is :我做的是:

Apache Nifi replaceText 示例

I use ReplaceText with Replacement Strategy : Regex Replace and Replacement Value : a regex Expression .我使用 ReplaceText with Replacement Strategy : Regex Replace and Replacement Value : a regex Expression This will replace FlowFile's matched content with this attribute's value and then you could continue your process.这将使用此属性的值替换 FlowFile 的匹配内容,然后您可以继续您的过程。

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

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