Converting a Large JSON File into CSV
Apache NiFi 1.0.0 has a lot of cool features, but no JSON-to-CSV converter yet.
For my use case, I wanted to split one JSON file that had 5,000 records into 5,000 CSVs. You could also through a MergeContent processor in there and make one file. My data came from the excellent test data source, RandomUser. They provide a free API to pull down data, https://api.randomuser.me/?results=5000&format=pretty. I chose pretty to get formatted JSON with multiple lines which is easier to parse.
This is an example of JSON returned from that service:
{"results":[
{"gender":"male",
"name":{"title":"monsieur","first":"lohan","last":"marchand"},
"location":{"street":"6684 rue jean-baldassini","city":"auboranges","state":"schwyz","postcode":9591},
"email":"lohan.marchand@example.com",
"login":{"username":"biggoose202","password":"esther","salt":"QIU1HBsr","md5":"9e60da6d4490cd6d102e8010ac98f283","sha1":"3de3ea419da1afe5c83518f8b46f157895266d17","sha256":"c6750c1a5bd18cac01c63d9e58a57d75520861733666ddb7ea6e767a7460479b"},
"dob":"1965-01-28 03:56:58",
"registered":"2014-07-26 11:06:46",
"phone":"(849)-890-5523",
"cell":"(395)-127-9369",
"id":{"name":"AVS","value":"756.OUVK.GFAB.51"},
"picture":{"large":"https://randomuser.me/api/portraits/men/69.jpg","medium":"https://randomuser.me/api/portraits/med/men/69.jpg","thumbnail":"https://randomuser.me/api/portraits/thumb/men/69.jpg"},
"nat":"CH"}
]
Step 1: GetFile: Read the JSON file from a directory (I can process any JSON file in there)
Step 2: SplitJSON: Split on $.results to split array of JSON.

Step 3: EvaluateJSONPath: Pull out attributes from the JSON record sent. Example: $.cell

Step 4: UpdateAttribute: Update the filename to be unique ${md5}.csv.

Step 5: ReplaceText: To format JSON attributes into a line of command-separated values.

Step 6: PutFile: Store the resulting file in a directory. (This could also be PutHDFS or many other sink processors).


