Skip to content

Repository files navigation

json2csv

Json -> CSV conversion utility

json2csv is able to convert arbitrary newline-delimited JSON (.ndjson) into a valid CSV. Currently, json2csv supports newline-delimited JSON only.

Build json2csv

You need to install Stack

then just execute stack build

Usage

json2csv in.ndjson out.csv +RTS -N

Examples

nested arrays in single document

JSON input:

{"a": [{"b": "b1","c": [1,2]},{"b": "b2","c": [3,4]}],"d": [10,20]}

CSV output:

a.$.ba.$.c.$d.$
b11.010.0
b11.020.0
b12.010.0
b12.020.0
b23.010.0
b23.020.0
b24.010.0
b24.020.0
various types of inner values

JSON input:

{"a": "field a0","b": [{"value": "field b2"},{"value": "field b xxx"}],"c": [{"value": "field c0"},{"value": "field c xxx"}]}{"a": "field a1","b": [{"value": "field b1"}],"c": [{"value": "field c1"}]}{"a": "field a2","b": [{"value": "field b2"},{"value": "field b xxx"}],"c": {"value": "field c2"},"d": 42}

CSV output:

ab.$.valuec.$.valuedc.value
field a0field b2field c0
field a0field b2field c xxx
field a0field b xxxfield c0
field a0field b xxxfield c xxx
field a1field b1field c1
field a2field b242.0field c2
field a2field b xxx42.0field c2
flattening

If You don't need to know, which values came from arrays, You can use -f (--flatten)

For the same JSON input it will give the following output:

c.valueadb.value
field c0field a0field b2
field c0field a0field b xxx
field c xxxfield a0field b2
field c xxxfield a0field b xxx
field c1field a1field b1
field c2field a242.0field b2
field c2field a242.0field b xxx
intersection

You can extract only common fields from all lines with -i (--intersect)

For the same JSON input it will give the following output:

ab.$.value
field a0field b2
field a0field b xxx
field a1field b1
field a2field b2
field a2field b xxx

About

Json -> CSV conversion utility

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages