ndjson-to-csv
Crystal command line tool and library. It turns a file of JSON objects, one per line (NDJSON), into a CSV for the paths you name, and with --summary it prints how many cells are filled per column instead. It reshapes text you give: it fetches nothing, reads no website or API, and flattens only the paths you name, so a nested object you do not name stays one JSON cell or is left out. Paths: a dot goes into an object key (address.city), a whole number goes to an array index (tags.0). A key that itself contains a dot cannot be named. Without --columns the columns are the top level keys in order of first appearance. A missing path or a null is an empty cell, a string is its text, a number or true/false is its JSON text as Crystal prints it (12.5 stays 12.5, 1e3 becomes 1000.0), and an object or array chosen directly is compact JSON text. A cell is double quoted when it holds a comma, a double quote, a carriage return or a line feed (quotes are doubled); lines end with a line feed. The CSV is written by the tool itself. In the summary, filled means not missing, not null and not an empty string; shares have 1 decimal and round half away from zero.
Part of the OneGTMLab free GTM tools. Background on the idea: Bright Data.
Install
shards build # then ./bin/ndjson-to-csv
Use
Input: one JSON object per line. Blank lines are skipped and not counted (a UTF-8 BOM and CRLF line endings are fine). A line that is not valid JSON, or is valid JSON but not an object (a number, a list), stops the run with a message naming the line number and exit 1; with --skip-bad such lines are counted and skipped instead (a note goes to stderr, and the summary shows the count). A whole number too large for 64 bits makes its line bad. Options: --columns a,b.c,d.0 or --columns=..., --out FILE (write the CSV there instead of standard output), --summary (cannot be combined with --out), --skip-bad. A missing file, an empty file, an empty column name, an unknown option or a missing option value stops with a message and exit 1. In the example below the first command writes the CSV (the row with id 2 shows a quoted comma, a null city and an empty list, id 3 quoted double quotes, id 4 an empty name and a missing city) and the second command prints the filled counts for the same four columns.
$ bin/ndjson-to-csv sample.ndjson --columns id,name,address.city,tags.0 ; echo ; bin/ndjson-to-csv sample.ndjson --columns id,name,address.city,tags.0 --summary
id,name,address.city,tags.0
1,Alpha,City A,a
2,"Beta, Ltd",,
3,"Gamma ""G""",City B,x
4,,,only
lines read: 4
objects: 4
bad lines skipped: 0
columns: 4
column filled share
id 4 of 4 100.0%
name 3 of 4 75.0%
address.city 2 of 4 50.0%
tags.0 3 of 4 75.0%
Test
crystal spec
Offline, no network calls, no dependencies beyond the language runtime. MIT licensed.