NiFi how do I get JSON array of string converted to comma separated plain text value and substring(1024)

huangapple go评论55阅读模式
英文:

NiFi how do I get JSON array of string converted to comma separated plain text value and substring(1024)

问题

尝试了5小时,试图将一个包含字符串值的JSON数组转换为普通字符串,用逗号分隔值并通过子字符串修剪长度。

NiFi可以做到吗?

例如,从以下开始:

[
    "Charlie was here",
    "Linus was here",
    "Snoopy was here",
    "Sally was here"
]

我尝试将其转换为:

Charlie was here,Linus was here,Snoopy was here,Sally was here

因此,如果上述值被存储到名为'myData'的属性中,

然后我可以对它进行子字符串操作,以缩短总长度,不管最后被截掉什么。
例如:
myData:substring(0,1024)

我尝试使用以下处理器,各种组合,但没有找到正确的处理器。

  • UpdateAttribute
  • EvaluateJSONPath
  • SplitJSON
  • MergeContent

我最接近的是使用splitjson和mergecontent,但然后内容中没有逗号分隔值,我最终得到了:

Charlie was hereLinus was hereSnoopy was hereSally was here

我在这里找到的几乎所有内容都涉及将文本转换为JSON,而不是将JSON转换为文本。

我错过了哪个处理器?

英文:

Been at this for 5 hours trying to convert a JSON array of string values to a plain string comma separated value and trim the length via substring.

Can NiFi do this?

e.g.
Starting with

[
    "Charlie was here",
    "Linus was here",
    "Snoopy was here",
    "Sally was here"
]

I am trying to convert it to

Charlie was here,Linus was here,Snoopy was here,Sally was here

So if the above value gets stored into an attribute called 'myData'

then I can substring it to shorten the overall length and it does not matter what gets chopped off at the end.
e.g.
myData:substring(0,1024)

I have been trying to use the following processors, various combinations but have not been able to find the correct one to use.

  • UpdateAttribute
  • EvaluateJSONPath
  • SplitJSON
  • MergeContent

The closest I got is with the splitjson and mergecontent but then the content contains no comma separating the values and I end up with

Charlie was hereLinus was hereSnoopy was hereSally was here

Just about everything I have found posted in here deals with text convert to json but not json convert to text.

What processor am I missing here?

答案1

得分: 1

一个选择是使用JoltTransformJSON处理器,具体规范如下:

[
  {
    "operation": "shift",
    "spec": {
      "*": "&1[]"
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": "=join(',',@(1,&))"
    }
  },
  {// 仅导出扁平化后的字符串
    "operation": "shift",
    "spec": {
      "*": ""
    }
  }
]

该网站上的演示地址为:NiFi how do I get JSON array of string converted to comma separated plain text value and substring(1024)

英文:

An option would be using JoltTransformJSON processor with the following specification :

[
  {
    "operation": "shift",
    "spec": {
      "*": "&1[]"
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": "=join(',',@(1,&))"// concatenate all string components separated  by comma
    }
  },
  {// derive the unnested string only 
    "operation": "shift",
    "spec": {
      "*": ""
    }
  }
]

the demo on the site http://jolt-demo.appspot.com/ is :

NiFi how do I get JSON array of string converted to comma separated plain text value and substring(1024)

huangapple
  • 本文由 发表于 2023年2月8日 22:41:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75387397.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定