Created 01-23-2019 10:16 AM
Hi, I have a json object as input, however one of its elements is a string that contains keys and values.
I would like to convert this string to json.
For example, my input json is:
{"name" : "John",
"empID" : "1234",
"additionl_information" : "age=29;height=1.80"
}
And the output should be:
{"name" : "John",
"empID" : "1234",
"age" : "29",
"height" : "1.80"
}
The key/value pairs are delimited with ";".
And most important is that we don't know in advance what will be the keys inside the "addition_information" parameter. We have "age" and "height" in one event, but in another event we can get other information.
Many thanks in advance.
Created on 01-23-2019 02:22 PM - edited 08-17-2019 02:48 PM
You can use a replacetext processor with a regular expression as in this example I did for you
Example input flowfile
After the replacetext processor with the above configuration
Created on 01-23-2019 02:26 PM - edited 08-17-2019 02:48 PM
You have to use a replace text Processor and configure it in the following way
ReplaceText Processor will replace content in the flowfile based on a regular expression match.
Everything that matches the regular expression will be replaced.
The key of the regular expression is the use of parentheses (), in the way that the content of the first parentheses can be referenced later as $1 and the content of the second parentheses can be referenced as $2.
Based on your example this is the content of the incoming flowfile
And this the result after the ReplaceText processor with the above configuration
Created 01-23-2019 02:30 PM
Thank you @Mariano Billinghurst
One of the challanges that I have is that I don't know in advance what are the key names. So in the above example, I don't know in advance that I will get inside the "additional_information" the "age" and "hight" keys. This part is dynamic.
Do you know how it can be supported?
Created 01-24-2019 12:24 PM
Then use several replacetext processor in a chain
1st Processor
replace :
"additionl_information" :with:
(empty string)
2nd processor
replace:
=
with:
" : "
3rd Processor
replace:
;
with:
"(newline)
note: for the (newline) you should actually should hit shift+enter to get the new line in the nifi processor