-
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.
@EdGaere, thanks for the example! I thought I could improve (i.e., shorten) the command you wrote. In your example, you called
jq
twice in the pipeline, but it can be done with one call instead…I.e., combining the filters:
.data
+.[]
→.data[]
.
I wanted to test this with your data. `jq` didn't like your hand-edited data's unquoted keys, like `some_other_field`, so I cleaned up the data first…Cleaning the test data
(Maybe
jq
has some option to ignore errors like unquoted keys.)
Running the shortened command I gave above gives the output…Using the test data
Notice that the
-c
option forjq
compacts the output without whitespace in each record. It's more compact than the hand-edited output of your example.