I want to convert the text into JSON format using nifi

1k views Asked by At

I have been trying to convert my integer and string values to JSON format using replacetext processor in NIFI. But I'm facing problem in regular expression. Can anyone suggest me a Regular Expression in search value and replacement value.

Orginal Text format :

{Sensor_id:2.4,locationIP:2.2,Sensor_value:A}

Expected JSON format

{Sensor_id:2.4,locationIP:2.2,Sensor_value:"A"}

Processor configuration : enter image description here

1

There are 1 answers

7
rmesteves On BEST ANSWER

You can use the regex ([\w_]+):([a-zA-Z]\w*) with replacement $1:"$2" as you can see here

But notice that a valid JSON should have quotes in the keys. For example:

 {"Sensor_id":2.4,"locationIP":2.2,"Sensor_value":"A"}

In this case, I would recommend:

  1. Add a ReplaceText processor with the regex ([\w_]+): and replacement "$1":
  2. Link the output of the first ReplaceText to another ReplaceText processor with the regex ([\w_"]+):([a-zA-Z]\w*) and replacement $1:"$2"

I hope it helps

EDIT:

If you want to transform {Sensor_id:2.4,locationIP:2.2,Sensor_value:A} into {"Sensor_id":"2.4","locationIP":"2.2","Sensor_value":"A"} you can use only one regex in a single processor:

Regex: ([\w_]+):([.\w]*) Replacement: "$1":"$2"