Our Community is getting an upgrade! To get everything ready for the relaunch, we’ll be placing the site in read-only mode starting September 21st.
We really appreciate your understanding while we get things set up behind the scenes. Catch up on all the exciting details about the move here.
Need help or have questions? Drop us a line at [email protected]

Support Questions

Find answers, ask questions, and share your expertise
Announcements
Share your experience with Cloudera on G2 and get a $25 Amazon Gift card.
Hi, I'm CLEO! Something exciting is coming to the Community. Stay Tuned!

I have the text file which has key and value. How to convert the that to the JSON object in NIFI

avatar
Contributor

Hi,

 

I have the below data:

 

Record 6|Invalid values for Entry Date.
Record 7|Processing failed due to error in record: 6
Record 8|Processing failed due to error in record: 6
Record 9|Processing failed due to error in record: 6
Record 10|Processing failed due to error in record: 6
Record 11|Duplicate of record Job id  : , data id : , first_name: , last_name: , Date: 2021, Entry Date: 2021.
Record 12|Processing failed due to error in record: 11
Record 13|Processing failed due to error in record: 11
Record 14|Processing failed due to error in record: 11
Record 15|Processing failed due to error in record: 11

 

I want to convert it like:

 

[
{
"Record 6":"Invalid values for Entry Date."
},
{
"Record 7":"Processing failed due to error in record: 6"
},
{
"Record 8":"Processing failed due to error in record: 6"
},
{
"Record 9":"Processing failed due to error in record: 6"
},
{
"Record 10":"Processing failed due to error in record: 6"
},
{
"Record 11":"Duplicate of record Job id  : , data id : , first_name: , last_name: , Date: 2021, Entry Date: 2021."
},
{
"Record 12":"Processing failed due to error in record: 11"},
{
"Record 13":"Processing failed due to error in record: 11"},
{
"Record 14":"Processing failed due to error in record: 11"},
{
"Record 15":"Processing failed due to error in record: 11"}]

 

How can I do that. Please help 

1 ACCEPTED SOLUTION

avatar
Expert Contributor

Hello,

 

quick and dirty solution:

In Apache Nifi there you can find the SplitText Processor, there you can set the Line Split Count to 1.

Those splitted Flowfiles get some fragment attributes to merge them together again. So after splitting you can merge directly again with MergeContent Processor.

 

For this processor you set properties like:

- Merge Strategy: Defragment

- Delimiter Strategy: Text

- Header: [{"

- Footer: "}]

- Demarcator: "},{"

 

After that we can split key values with ReplaceText Processor with following properties what needs to be changed from default:

- Search Value: \|

- Replacement Value: ":"

 

Greetings

View solution in original post

2 REPLIES 2

avatar
Expert Contributor

Hello,

 

quick and dirty solution:

In Apache Nifi there you can find the SplitText Processor, there you can set the Line Split Count to 1.

Those splitted Flowfiles get some fragment attributes to merge them together again. So after splitting you can merge directly again with MergeContent Processor.

 

For this processor you set properties like:

- Merge Strategy: Defragment

- Delimiter Strategy: Text

- Header: [{"

- Footer: "}]

- Demarcator: "},{"

 

After that we can split key values with ReplaceText Processor with following properties what needs to be changed from default:

- Search Value: \|

- Replacement Value: ":"

 

Greetings

avatar
Contributor

@Faerballert ,

 

It's working fine as expected. Thank you so much for the quick response.