Skip to main content
Techie123
Silver ⭐️⭐️⭐️
Nov 22, 2022 12:17 PM UTC
Solved

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

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 

Faerballert
Gold ⭐️
Nov 23, 2022 10:40 AM UTC

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

2 replies

Faerballert
Gold ⭐️
Nov 23, 2022 10:40 AM UTC

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

Translate
Techie123
Techie123Author
Silver ⭐️⭐️⭐️
Nov 23, 2022 10:51 AM UTC

@Faerballert ,

 

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

 

Translate