Nikolay Smelov
Nikolay Smelov

Reputation: 59

How to convert a text file stream to a json file stream

I'm using apache nifi and I have a stream of text files that comes from a splitText processor with the following content, 2 lines: the first line is the id and the second line is the word. For example:

1
games

and I need to make json from this file, for example:

{
"id": 1,
"word": "games"
}

I tried to enter the values ​​from the lines into the file attributes using updateAttribute, but I don’t know how to access the first and second lines, as I understand they are separated by the “\n” character, but I don’t know how to get exactly the first line to create the id, and how to get the second row for the word attribute. If I can make attributes, then I can make json from attributes using AttributesToJSON

Thank you in advance for your cooperation!

Upvotes: 2

Views: 100

Answers (1)

samsal77
samsal77

Reputation: 759

You can use ExtractGrok processor , its good for reading some unstructured data and generate json output. You can read more about the Grok Expression here:

GrokExperssio

For your case I used the following Expression:

%{INT:id}\n%{WORD:word}

enter image description here

This gave me the following json output:

{"id":"1","word":"games"}

You can use jolt transformation to convert id to integer using the spec:

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "id": "=toInteger(@(1,id))"
    }
  }
]

Hope that helps

Upvotes: 0

Related Questions