module NdjsonToCsv

Overview

Reshape a file of JSON objects (one per line) into CSV text for the paths you name. Nothing is fetched, nothing is guessed: a path that is not in an object gives an empty cell. The CSV is written by hand, no "csv" library.

Defined in:

ndjson_to_csv.cr

Constant Summary

USAGE = "usage: ndjson-to-csv file.ndjson [--columns id,name,address.city,tags.0] [--out FILE] [--summary] [--skip-bad]\nOne JSON object per line. A dot goes into an object key, a number goes to an array index. It reshapes text you give; it fetches nothing and flattens only the paths you name.\nMore: https://www.onegtmlab.dev/tool-directory/research-and-scraping/brightdata"
VERSION = "1.0.0"

Class Method Summary

Class Method Detail

def self.cell(value : JSON::Any | Nil) : String #

Missing or null is empty. A string is its text. A number or boolean is its JSON text. An object or array is compact JSON text.


[View source]
def self.cli(args : Array(String), stdout : IO, stderr : IO) : Int32 #

Command line entry. Returns the exit code: 0 ok, 1 bad input or file, 2 usage.


[View source]
def self.csv(objects : Array(Obj), columns : Array(String)) : String #

[View source]
def self.default_columns(objects : Array(Obj)) : Array(String) #

Top level keys in order of first appearance.


[View source]
def self.lookup(obj : Obj, path : String) : JSON::Any | Nil #

Follows a path through one object. nil means the path is not there.


[View source]
def self.parse(text : String, skip_bad : Bool = false) : Parsed #

Reads the text line by line. Blank lines are skipped (and not counted). A line that is not valid JSON, or is valid JSON but not an object, is bad: an error naming the line number, or counted and skipped with skip_bad.


[View source]
def self.parse_columns(text : String) : Array(String) #

Comma list of paths. A dot goes into an object key, a whole number goes to an array index. Empty names and empty path parts are errors.


[View source]
def self.percent1(part : Int32, whole : Int32) : String #

tenths of a percent, rounded half away from zero (counts are never negative)


[View source]
def self.quote(text : String) : String #

[View source]
def self.summary(parsed : Parsed, columns : Array(String)) : String #

[View source]
def self.table(rows : Array(Array(String))) : String #

[View source]