Support Questions

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

Text processing of FlowFiles content in Apache NiFi

avatar
Explorer

Hi All,

    I have single JSON object in a flow file. I just want to append and prepend text to it, and pass it over as flowfiles for subsequent processors. Here is what I want to achieve:

 

Input FlowFile Content:

{ "first_name": "alpha", "age": "30" }

Expected output in FlowFile Content:

{ $set: { "first_name": "alpha", "age": "30" } }

 

So basically i want to prepend "{ $set " and append " } "

I found crude way of updating it, in multiple steps. But just wanted to do it in proper and concise manner.

5 REPLIES 5

avatar
Super Guru

@arunnalpet This sounds like a perfect job for ReplaceText:

 

https://nifi.apache.org/docs/nifi-docs/components/org.apache.nifi/nifi-standard-nar/1.5.0/org.apache...

 

I did notice that you have set as $set.  In order to maintain a real json object, I have used just "set".  The set object is available as $.set if you use EvalateJsonPath.   If you still need $set, please explain... 

 

 

Below is the configuration you need for ReplaceText.  It regex matches the entire flow file then adds the prepend before $1 and adds the append after.

 

{ "set" : $1 }

(this is the only change in the processors from default)

 

Screen Shot 2020-04-28 at 7.46.49 AM.png

 

 

And the output:

 

{ "set" : { "first_name": "alpha", "age": "30" } }

 

avatar
Explorer

Thank you! This gives me some directions.

One thing though is that, i really want it to be "$set" and I am not expecting it to comply to JSON standard after doing this.

 

So I guess i can just use $set in the replacement value and get the job done. (i think we need to escape $ value). Will try it out and get back.

avatar
Super Guru

@arunnalpet   yes that is correct.  I wanted to avoid $set in basic testing and also advocating for valid code practice to avoid issues further with the data source.    

 

 

If the answer above resolves your issue or allows you to move forward, please choose to ACCEPT this solution and close this topic. If you have further dialogue on this topic please comment here or feel free to private message me. If you have new questions related to your Use Case please create separate topic and feel free to tag me in your post.  

 

Thanks,


Steven @ DFHZ

avatar
Explorer

Hi,

      Actually I am working on MongoDB queries, which supports 'Extended JSON' as opposed to 'Standard JSON'. For which I need to enforce $set. Is there a way we can implement this?

avatar
Super Guru

@arunnalpet You should be able to just add the $. If it doesn't work, maybe have to escape it:  \$