3

In NIFI how to convert from CSV to JSON without CSV header. For each CSV row searate json flow file sholud create and send to next processor. Below is the csv

861359032561480,1,15.480237,190506144035,a
861359032561480,3,16.480237,190506144035,b
861359032561480,2,17.480237,190506144035,c

The expected json is :

{"test":861359032561480,"test2":"1","test3":15.48,"test4":190506144035,"test5":A}

In the above JSON second json value 1 is string, in third json value the value is restricted to 2 digits after the decimal, in fourth json value is uppercased.

So, how can I convert and apply these transformations?

1 Answer 1

3

Use QueryRecord(to convert field to uppercase) processor and then Split the array of json records.

  • Configure Record Reader(CSVReader)/Writer(JsonRecordSetWriter).

  • In JsonSetWriter keep the matching avro schema like long,string,decimal.

In QueryRecord processor add the new property and keep the sql statement to convert "test5" to UPPER(test5) case, using ApacheCalcite sql parser.

Your sql statement would be something like below..

select test,test2,test3,test4,UPPER(test5)test5 from flowfile

Then use SplitRecord(prefered if json file is big) (or) SplitJson processors to split the array of json records to individual flowfiles.

Flow:

1.QueryRecord //to read csv and write in json format
2.SplitRecord (or) SplitJson //to Split array into individual flowfiles
3.other processors.
Sign up to request clarification or add additional context in comments.

4 Comments

How can I add csv header because the csv file is not having header
@vikram, We don't have to add csv header but while configuring CSVReader we need to configure Avro Schema with field names. then processor will read the csv file with the specified schema. in case if u want to add header then check out this link: community.hortonworks.com/questions/191116/…
If i am trying without header every field is coming as null
@vikram You can use the InferAvroSchema processor and make it place the avro schema of the CSV into a flowfile attribute. Then in your CSV reader simply change the access strategy to schema text and use the inferred avro schema. In InferAvroSchema you can even hardcode the csv header line yourself

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.