【问题标题】:How to convert a string to a JSON array using NiFi如何使用 NiFi 将字符串转换为 JSON 数组
【发布时间】:2019-06-26 08:45:00
【问题描述】:

在 NiFi 中,我正在处理包含以下属性的流文件:

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

我想在这个数组上拆分 flowFile 以分别处理每个元素(然后合并)。我尝试使用SplitJson处理器,但它需要JSON内容才能操作,所以我在它之前使用了AttributesToJSON。不幸的是,产生的flowFile的内容是:

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

我收到了错误

$['my_array'] 的计算值 [u'firstElement', u'secondElement'] 不是 JSON Array 兼容类型,无法拆分。

是否可以将 my_array 字符串转换为正确的 JSON 数组?我需要使用ExecuteScript 还是有更简单的方法?

【问题讨论】:

  • [u'firstElement', u'secondElement'] 的 json 格式无效。我认为这是一种 python 格式。
  • 是的,这些值来自 Python,但我认为主要问题是使用 "[u'firstElement', u'secondElement']" 而不是 [u'firstElement', u'secondElement']
  • 带双引号 - 它只是一个字符串。没有 - 不是有效的 json。你能从 python 生成 json 吗?
  • 我尝试将其转换为Python中的列表,但它导致错误org.python.core.PyList cannot be cast to java.lang.String ,因此Python脚本似乎必须返回字符串。
  • 使用json.dumps(...)将python数据转换为json格式的字符串

标签: apache-nifi apache-minifi


【解决方案1】:

ReplaceTextAlways ReplaceReplacement Strategy${my_array}Replacement Value 然后SplitJSON 怎么样? 这将用这个属性的值替换你的 FlowFile 的内容,然后你可以SplitJSON 就可以了。

【讨论】:

  • 谢谢,我使用了您的解决方案以及@dagett 提出的解决方案。我更改了 Python 脚本以将json.dumps(...) 设置为属性值,然后将ReplaceTextReplacement Value = {"my_array": ${my_array}} 放在一起
【解决方案2】:

假设我想将字符串:“Hashtags”:“['tag1','tag2']”(我在 Nifi 中生成的 json 的一部分,)更改为:“Hashtags ": ['tag1','tag2']。

我所做的是:

我将 ReplaceText 与 Replacement Strategy : Regex ReplaceReplacement Value : a regex Expression 一起使用。这将用该属性的值替换 FlowFile 的匹配内容,然后您可以继续您的过程。

【讨论】:

  • 确实有效,感谢您的提示。 NiFi 没有正确格式化 AttributesToJson 处理器中的 JSON 列表,而是将它们转换为字符串,这很糟糕。
猜你喜欢
  • 2021-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-20
  • 2015-04-05
  • 2011-11-22
  • 2022-01-27
相关资源
最近更新 更多