Search code examples
jsonapache-nifijolt

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


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?


Solution

  • 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 :

    enter image description here