Support Questions

Find answers, ask questions, and share your expertise

How to transform a json array into a string list with nifi

avatar

Hello,

i have a queryCassandra which generate a json like this one:

{"results":[{"term":"term1"},{"term":"term2"}..]} 

Now, i want to get from this all the terms values separated by some separator in string format;

ex : term1,term2,term3

So i can pass this list as a string parameter for a java main program which i've alreat set.

(i only need the transofrmation, not the java program execution) Thank you !

1 ACCEPTED SOLUTION

avatar

Hello,

I think you can do that with EvaluateJsonPath extracting String in an JSON array, followed by UpdateAttribute removes special characters such as double quotes and square brackets by Expression Language.

Here is an example NiFi template that does above conversion:

https://gist.github.com/ijokarumawak/bd37690ee8aa0bf8e9e2a47cf064e0d4

15654-json-array-to-string-list.png

View solution in original post

5 REPLIES 5

avatar
New Contributor

first you need to validate JSON data , you can use this tool JSON Formatter and JSON Validator

avatar

Hello,

I think you can do that with EvaluateJsonPath extracting String in an JSON array, followed by UpdateAttribute removes special characters such as double quotes and square brackets by Expression Language.

Here is an example NiFi template that does above conversion:

https://gist.github.com/ijokarumawak/bd37690ee8aa0bf8e9e2a47cf064e0d4

15654-json-array-to-string-list.png

avatar

thanks @kkawamura! a last question, how can i get the flowfile generated as ex : term1,term2,term3 as a parameter in the next processor?

avatar

You mean how to map the attribute value (term1,term2,term3) to FlowFile content? If so, I'd use ReplaceText processor, set Replacement Value as "${attribute-name}" and Replacement Strategy as "Always Replace".

avatar
New Contributor

In my case I have to fetch a particular key value from a json array. I was trying to do $.[key] but it just splits the array into separate json files. Is there any solution for this ? I wanted the split array as {"test1", "test2"}

Jsonarray example: [{"key":"test1", "name":"testName"}, {"key":"test2", "name":"testName2"}]