Support Questions

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

NIFI JSON event extraction only integers from string

avatar
Explorer

I have a JSON data coming as {"x":"avc123.abc.com"} I would like to have a processor in nifi to extract only the integer (123) from the entire value and output it from the process

secondly, I would like that integer value (123) to be added in the JSON event with different keys such as the final JSON event would be {"x":"avc123.abc.com" "y":"123" }

I already tried routetext and extract text but it didn't work.

2 ACCEPTED SOLUTIONS

avatar
Master Guru

@ANMAR 

You need to use ExtractText processor and matching regex to extract only the integer value.

---------------------------------------------

Add new property in ExtractText processor 

val

 

 

(\d+)

 

 

 -

Then use ReplaceText processor with below configs:

Search Value

}
Replacement Value
,"y":"${val}"}
Character Set
UTF-8
Maximum Buffer Size
1 MB
Replacement Strategy
Literal Replace
Evaluation Mode
Entire text

-

By using Replacetext processor we are extracting the value and adding "y" key with the extracted value.

--------------------------------------------

Input data:

 

 

{"x":"avc123.abc.com"}

 

 

 Output:

 

 

{"x":"avc123.abc.com","y":"123"}

 

 

 

View solution in original post

avatar
Master Guru

@ANMAR 

 

Try with this regex in ExtractText processor.

(?:"x":.\w+?)(\d+)

This regex will extract only the digit in "x" key and adds that value for "y" key in ReplaceText processor.

View solution in original post

5 REPLIES 5

avatar
Master Guru

@ANMAR 

You need to use ExtractText processor and matching regex to extract only the integer value.

---------------------------------------------

Add new property in ExtractText processor 

val

 

 

(\d+)

 

 

 -

Then use ReplaceText processor with below configs:

Search Value

}
Replacement Value
,"y":"${val}"}
Character Set
UTF-8
Maximum Buffer Size
1 MB
Replacement Strategy
Literal Replace
Evaluation Mode
Entire text

-

By using Replacetext processor we are extracting the value and adding "y" key with the extracted value.

--------------------------------------------

Input data:

 

 

{"x":"avc123.abc.com"}

 

 

 Output:

 

 

{"x":"avc123.abc.com","y":"123"}

 

 

 

avatar
Explorer

@Shu_ashu  Thank you very much! You saved the day for me. 

avatar
Explorer

How about when i have multiple fields such as {
"x":"avc123.abc.com",
"z":"24 - ny"
}
and I would like to only targer "x" value for "123" and the rest won't get affected such as "z"
so the expected output would be {
"x":"avc123.abc.com",
"z":"24 - ny",
"y":"123"
}

Please note that the "y" value only targeted the "x" and the z shouldn't be picked up?
 

avatar
Explorer

@Shu_ashu  I will wait for your response my friend. Thanks! 

avatar
Master Guru

@ANMAR 

 

Try with this regex in ExtractText processor.

(?:"x":.\w+?)(\d+)

This regex will extract only the digit in "x" key and adds that value for "y" key in ReplaceText processor.