Support Questions

Find answers, ask questions, and share your expertise

NIFI extract text from JSON

avatar
New Contributor

I'm using the NIFI ExtractText Processor and I'm trying to come up with the regular expression to extract a field value from a JSON String. Sample JSON Data is:

{"ID": 10649, "ORDER_ID": 85, "PRODUCT_ID": "JT8382X38", "CUSTOMER_ID": 57, 
 "QUANTITY": 2, "ORDER_DATE": "2018-02-17 11:09:56.217", "REGION": "Far East", 
 "NOTES": "Repeat Purchase", "AMOUNT": "7997.48", "STATUS": 105}

I want to extract the ID field value (10649). What would be a valid regular expression to get this value?
Thanks!

3 REPLIES 3

avatar
Master Guru

@Chris R

In Extract Text processor add new property as

ID

Regex:-

"ID":(.*?),

63385-extracttext.png

(or)

As you are having json message instead of Extract text processor use Evaluatejson processor and change below properties

Destination

flowfile attribute 

add new property as

ID

$.ID

will add new attribute called ID as flowfile attribute.

Configs:-

63386-evaljson.png

avatar
Master Guru

@Chris R

To match white space use the below regex

ID

"ID":\s(.*?),

\s matches any whitespace character (equal to [\r\n\t\f\v ]).

You can validate your regex here regex

We need to match whitespaces if we are using extract text processor,don't need to match any whitespace characters if you are using Evaluatejson path processor to extract content and keep it as attribute to the flowfile.

avatar
New Contributor

@Shu

Thanks very much, both of those solutions worked.

Question:
When using the ExtractText processor, how can I account for whitespace between the colon : and the actual ID string?
This does not seem to work:

"ID":(\w+)(.*?),

If the start of my string is:
{"ID": 12345,

and I want to get the value 12345

Thanks!