Support Questions

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

How to convert key value string to json in NiFi

avatar
New Contributor

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.

4 REPLIES 4

avatar

@Yoel Barsheshet

You can use a replacetext processor with a regular expression as in this example I did for you

99393-screen-shot-2019-01-23-at-24452-pm.png

99394-screen-shot-2019-01-23-at-24402-pm.png

Example input flowfile

99395-screen-shot-2019-01-23-at-24508-pm.png

After the replacetext processor with the above configuration

99396-screen-shot-2019-01-23-at-24532-pm.png

avatar

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.

99389-screen-shot-2019-01-23-at-24452-pm.png

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.

99390-screen-shot-2019-01-23-at-24402-pm.png

Based on your example this is the content of the incoming flowfile

99391-screen-shot-2019-01-23-at-24508-pm.png

And this the result after the ReplaceText processor with the above configuration

99392-screen-shot-2019-01-23-at-24532-pm.png

avatar
New Contributor

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?

avatar

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