-
jq — https://jqlang.org/ — "like sed for JSON data"
There are several options available for installing jq. I prefer to use Homebrew:
brew install jq
-
jq -s '.' input.jsonl > output.json
-
jq -c '.[]' input.json > output.jsonl
Note: This document is now included in Cookbook · jqlang/jq Wiki.
It's unbelievable, jq can solve this in one line. I was about to embark on writing yet-another-python-script.py to convert a gzipped nested JSON file to JSONL, but thankfully I came across this post.
Suppose you have a JSON like this:
You can use the following one-line command to extract the 'data' array, keep the 'input' and 'target' fields only, and generate JSONL:
gunzip -c somefile.json.gz | jq .data | jq -c '.[] | {input, target}'
Output