Support Questions

Find answers, ask questions, and share your expertise
Announcements
Celebrating as our community reaches 100,000 members! Thank you!

how can i replace ',' with new line in replacetext processor

avatar
Contributor

I want to parse json response in nifi processor I have json data like this ,I have already used this expression inside evaluatejsonpath processor : $.Data['row'] and thanks to it i got row data then i have used another expression inside replacetext processor: [] to get rid of this '[]' but i can't replace ',' with new line how can i do this?

{"squadName":"Super hero squad","homeTown":"Metro City","formed":2016,"secretBase":"Super tower","active":true,"Data":{"row":[{"name":"Molecule Man","age":29,"secretIdentity":"Dan Jukes","powers":["Radiation resistance","Turning tiny","Radiation blast"]},{"name":"Madame Uppercut","age":39,"secretIdentity":"Jane Wilson","powers":["Million tonne punch","Damage resistance","Superhuman reflexes"]},{"name":"Eternal Flame","age":1000000,"secretIdentity":"Unknown","powers":["Immortality","Heat Immunity","Inferno","Teleportation","Interdimensional travel"]}]}

and i want totransform it into this format:

<code>{"name":"Molecule Man","age":29,"secretIdentity":"Dan Jukes","powers":["Radiation resistance","Turning tiny","Radiation blast"]}{name": "MoleculeMan", "age": 29,  "secretIdentity": "DanJukes", "powers":  ["Radiation resistance", "Turning tiny", "Radiation blast"]}
   {"name": "MoleculeMan", "age": 29,  "secretIdentity": "DanJukes", "powers":  ["Radiation resistance", "Turning tiny", "Radiation blast"]}

1 ACCEPTED SOLUTION

avatar
Master Guru

Hi @sally sally,
as you said you are getting rid of [] in the flow file content but in my case i haven't get rid of [] you can use your existing functionality. After that use another replace text processor with search value property as

(?<=]})(,)

if you are having any spaces or new line characters before , then make use of below regex

(?<=]})(\s*,)

and replacement value property as

shift+enter
it will replaces your , after } with new line

Input:-

   {
"squadName": "Super hero squad",
"homeTown": "Metro City",
"formed": 2016,
"secretBase": "Super tower",
"active": true,
"Data": {
"row": [{
"name": "Molecule Man",
"age": 29,
"secretIdentity": "Dan Jukes",
"powers": ["Radiation resistance",
"Turning tiny",
"Radiation blast"]
},
{
"name": "Madame Uppercut",
"age": 39,
"secretIdentity": "Jane Wilson",
"powers": ["Million tonne punch",
"Damage resistance",
"Superhuman reflexes"]
},
{
"name": "Eternal Flame",
"age": 1000000,
"secretIdentity": "Unknown",
"powers": ["Immortality",
"Heat Immunity",
"Inferno",
"Teleportation",
"Interdimensional travel"]
}]
}}
Output:-
[{"name":"Molecule Man","age":29,"secretIdentity":"Dan Jukes","powers":["Radiation resistance","Turning tiny","Radiation blast"]}
{"name":"Madame Uppercut","age":39,"secretIdentity":"Jane Wilson","powers":["Million tonne punch","Damage resistance","Superhuman reflexes"]} {"name":"Eternal Flame","age":1000000,"secretIdentity":"Unknown","powers":["Immortality","Heat Immunity","Inferno","Teleportation","Interdimensional travel"]}]


Replacetext configs:-

38656-replacetext-config.png

In addition
in future if you are expecting different flow files for each record make a use splitJson processor with
JsonPath Expression property as $.Data.row

connect the split property to another processor, it will results as in our array of data.row having 3 records so it will results 3 different flow files as a result from split json processor.

38657-splijson.png

Results after splitjson configs:-

38658-splitjson-result.png

Output:- as we are having 3 flow files above and the contents of those flowfiles as follows.
flowfile1:-

{"name":"Eternal Flame","age":1000000,"secretIdentity":"Unknown","powers":["Immortality","Heat Immunity","Inferno","Teleportation","Interdimensional travel"]}

flowfile2:-

{"name":"Molecule Man","age":29,"secretIdentity":"Dan Jukes","powers":["Radiation resistance","Turning tiny","Radiation blast"]}flowfile3:-
flowfile3:-
{"name":"Madame Uppercut","age":39,"secretIdentity":"Jane Wilson","powers":["Million tonne punch","Damage resistance","Superhuman reflexes"]}

View solution in original post

5 REPLIES 5

avatar
Contributor

To sum up i want to replace ',' after } with \n

avatar
Master Guru

Hi @sally sally,
as you said you are getting rid of [] in the flow file content but in my case i haven't get rid of [] you can use your existing functionality. After that use another replace text processor with search value property as

(?<=]})(,)

if you are having any spaces or new line characters before , then make use of below regex

(?<=]})(\s*,)

and replacement value property as

shift+enter
it will replaces your , after } with new line

Input:-

   {
"squadName": "Super hero squad",
"homeTown": "Metro City",
"formed": 2016,
"secretBase": "Super tower",
"active": true,
"Data": {
"row": [{
"name": "Molecule Man",
"age": 29,
"secretIdentity": "Dan Jukes",
"powers": ["Radiation resistance",
"Turning tiny",
"Radiation blast"]
},
{
"name": "Madame Uppercut",
"age": 39,
"secretIdentity": "Jane Wilson",
"powers": ["Million tonne punch",
"Damage resistance",
"Superhuman reflexes"]
},
{
"name": "Eternal Flame",
"age": 1000000,
"secretIdentity": "Unknown",
"powers": ["Immortality",
"Heat Immunity",
"Inferno",
"Teleportation",
"Interdimensional travel"]
}]
}}
Output:-
[{"name":"Molecule Man","age":29,"secretIdentity":"Dan Jukes","powers":["Radiation resistance","Turning tiny","Radiation blast"]}
{"name":"Madame Uppercut","age":39,"secretIdentity":"Jane Wilson","powers":["Million tonne punch","Damage resistance","Superhuman reflexes"]} {"name":"Eternal Flame","age":1000000,"secretIdentity":"Unknown","powers":["Immortality","Heat Immunity","Inferno","Teleportation","Interdimensional travel"]}]


Replacetext configs:-

38656-replacetext-config.png

In addition
in future if you are expecting different flow files for each record make a use splitJson processor with
JsonPath Expression property as $.Data.row

connect the split property to another processor, it will results as in our array of data.row having 3 records so it will results 3 different flow files as a result from split json processor.

38657-splijson.png

Results after splitjson configs:-

38658-splitjson-result.png

Output:- as we are having 3 flow files above and the contents of those flowfiles as follows.
flowfile1:-

{"name":"Eternal Flame","age":1000000,"secretIdentity":"Unknown","powers":["Immortality","Heat Immunity","Inferno","Teleportation","Interdimensional travel"]}

flowfile2:-

{"name":"Molecule Man","age":29,"secretIdentity":"Dan Jukes","powers":["Radiation resistance","Turning tiny","Radiation blast"]}flowfile3:-
flowfile3:-
{"name":"Madame Uppercut","age":39,"secretIdentity":"Jane Wilson","powers":["Million tonne punch","Damage resistance","Superhuman reflexes"]}

avatar
Contributor

thank you for your address , Do you know how can i replace "[{ ......}] with { .....} in replacte text i have tried this [\[\]](\{|\}) but it can annly remove first [ and leavs ] at the and of json data reuqest

avatar
Master Guru

In Replace Text Processor
set Search Value property as

^\[(.*)\]$

it will captures all the data except [] into group 1 then use

Replacement Value property as

$1

as a result from replace text processor you can get all the data except [] as a new replacement of the content.

screenshots of configs:-

40381-replacetext-confs.png

Input to Replace Text:-

[{"name":"Molecule Man","age":29,"secretIdentity":"Dan Jukes","powers":["Radiation resistance","Turning tiny","Radiation blast"]},{"name":"Madame Uppercut","age":39,"secretIdentity":"Jane Wilson","powers":["Million tonne punch","Damage resistance","Superhuman reflexes"]},{"name":"Eternal Flame","age":1000000,"secretIdentity":"Unknown","powers":["Immortality","Heat Immunity","Inferno","Teleportation","Interdimensional travel"]}]

Output after Replace Text:-

{"name":"Molecule Man","age":29,"secretIdentity":"Dan Jukes","powers":["Radiation resistance","Turning tiny","Radiation blast"]},{"name":"Madame Uppercut","age":39,"secretIdentity":"Jane Wilson","powers":["Million tonne punch","Damage resistance","Superhuman reflexes"]},{"name":"Eternal Flame","age":1000000,"secretIdentity":"Unknown","powers":["Immortality","Heat Immunity","Inferno","Teleportation","Interdimensional travel"]}

avatar
Contributor

thank you , your answer was very helpful 😄