https://github.com/danslapman/json2csv
Json -> CSV conversion utility
https://github.com/danslapman/json2csv
json2csv
Last synced: 3 months ago
JSON representation
Json -> CSV conversion utility
- Host: GitHub
- URL: https://github.com/danslapman/json2csv
- Owner: danslapman
- License: wtfpl
- Created: 2018-12-22T21:55:10.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2024-10-05T16:10:12.000Z (8 months ago)
- Last Synced: 2025-01-25T08:08:07.966Z (5 months ago)
- Topics: json2csv
- Language: Haskell
- Homepage:
- Size: 65.4 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: ChangeLog.md
- License: LICENSE
Awesome Lists containing this project
README
# json2csv
Json -> CSV conversion utilityjson2csv 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](https://docs.haskellstack.org/en/stable/README/#how-to-install)
then just execute `stack build`
### Usage
`json2csv in.ndjson out.csv +RTS -N`
### Examples
##### nested arrays in single document
JSON input:
```javascript
{"a": [{"b": "b1", "c": [1, 2]},{"b": "b2", "c": [3, 4]}], "d": [10, 20]}
```CSV output:
a.$.b|a.$.c.$|d.$
-----|-------|---
b1|1.0|10.0
b1|1.0|20.0
b1|2.0|10.0
b1|2.0|20.0
b2|3.0|10.0
b2|3.0|20.0
b2|4.0|10.0
b2|4.0|20.0##### various types of inner values
JSON input:
```javascript
{"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:
a|b.$.value|c.$.value|d|c.value
-|---------|---------|-|-------
field a0|field b2|field c0||
field a0|field b2|field c xxx||
field a0|field b xxx|field c0||
field a0|field b xxx|field c xxx||
field a1|field b1|field c1||
field a2|field b2||42.0|field c2
field a2|field b xxx||42.0|field 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.value|a|d|b.value
-------|-|-|-------
field c0|field a0||field b2
field c0|field a0||field b xxx
field c xxx|field a0||field b2
field c xxx|field a0||field b xxx
field c1|field a1||field b1
field c2|field a2|42.0|field b2
field c2|field a2|42.0|field 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:
a|b.$.value
-|---------
field a0|field b2
field a0|field b xxx
field a1|field b1
field a2|field b2
field a2|field b xxx