yukithm / json2csv

Convert JSON to CSV (go package and command line tool)
MIT License
83 stars 30 forks source link

Allow converting newline-delimited json via flag #19

Open vergenzt opened 3 weeks ago

vergenzt commented 3 weeks ago

What I'd like

Example input:

{"a":1,"b":2,"c":3}
{"a":4,"b":5,"c":6}
{"a":7,"b":8,"c":9}

Desired output:

a,b,c
1,2,3
4,5,6
7,8,9

Implementation ideas

Option 1. Only newline-delimited json

  1. Add -n/--ndjson.
  2. When passed, then read lines of input and parse each line as JSON.
  3. Apply --path resolution to each parsed line.
  4. Then handle the list of (--path-transformed) JSON-parsed lines as if they were a single JSON array.

Option 2. Accept "JSON streams"

  1. Add -s/--stream. (Or maybe still call the flag -n/--ndjson but do this behavior?)
  2. When passed, then read all input, and treat it as an (optionally whitespace-separated) concatenated stream of JSON values.
  3. Apply --path resolution to each parsed JSON value.
  4. Then handle the list of (--path-transformed) JSON-parsed values as if they were a single JSON array.

Thoughts? I'm open to implementing but figured I'd run it by you first.

I often finding myself converting trying to JSON to CSV and your tool is my go-to for its speed and simplicity! But it'd be nice to not have to wrap lines of JSON objects into an array before converting.