-1

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 ExtractText processors but it didn't work.

Pushkr
  • 3,591
  • 18
  • 31
Tr Ex
  • 61
  • 1
  • 6
  • See this https://stackoverflow.com/q/11339210/2308683 But what if there are multiple numbers like `a1v2c123`? Or multiple keys in the JSON? Is x always the key? – OneCricketeer Sep 08 '19 at 22:50
  • You may be able to achive this using 'EvaluateJson'(to extract value of x) + UpdateAttribute (to retrieve integer value) + AttributeToJson ( convert extracted attributes back to flowfile) – Pushkr Sep 09 '19 at 22:12

1 Answers1

0

I usually don't work with Json data, but I think this might solve your problem, although there might be a simpler way to do it.

import json

inpt  = '{"x":"avc123.abc.com"}' # json input.
def ExtractDigits(inpt):
        newstring =''
        dataList = json.loads(inpt) # convert into a python dictionary
        for string in dataList.values(): # loop thru its value
                for x in string:
                        if x.isnumeric():
                                newstring += str(x)
        return newstring

print(ExtractDigits(inpt)) 

You can then add this 'y':'123' to the inpt dictionary and then use json.dumps() to reconvert the data into json. Hope this helps you.

Bert_AT
  • 95
  • 8