Support Questions

Find answers, ask questions, and share your expertise

NiFi - How to extract key value from a string to JSON

avatar
New Contributor

Hi,

 

I am new to NiIf and need your help please.
I have a HandleHttpRequest processor and want to get the URL parameters into JSON.

The URL looks similar to this:

http://mydomain.de/getsomedata?key1=value1&key2=value2

 

but may can also be:

 

http://mydomain.de/getsomedata?myVar1=myValue1&myVar2=value2&lang=DE

So No. of parameter and parameter names and values can change of course.
Now I need a processor that extracts any of key/value pairs from http.query.string into:

{

  params: {

    "key1":"value1",

    "key2":"value2"

  }

}

 

or for 2nd example:

{

  params: {

    "myVar1":"value1",

    "myVar2":"value2",

    "lang":"DE"

  }

}

 

How can I achieve that? Is there any processor I can use or should I use ExecuteScript processor?

1 REPLY 1

avatar
Master Mentor

@MrBurns 

You want to take the URL that written to the FlowFile's attribute "http.request.uri" and generate a Json, correct?
Where do you want to write that JSON (a new FlowFile attribute?  Content of the FlowFile?)?

There are multiple ways to handle this.
If you just want to write JSON to a new FlowFile Attribute, you could use the "Advanced" UI of the UpdateAttribute by setting up a rule for each url type.

If you want to write to the content of a FlowFile, you could follow the above UpdateAttribute with a replaceText processor that does an "always replace" to write the json from the attribute to the content of the FlowFile.

another option here is to use a RouteOnAttribute to route each url type to a unique ReplaceText to handle the specific url type.  

I like first option since you can easily add new rules to the UpdateAttribute if any additional URL types are introduced without needing to modify the rest of your dataflow.

If you found that the provided solution(s) assisted you with your query, please take a moment to login and click Accept as Solution below each response that helped.

Thank you,

Matt