jsoncsv : easily convert json to csv or xls[x]
jsoncsv (with mkexcel) is a command tool to convert json file to csv/xlsx file.
It’s simple, and no need user to specify the keys.
Just use them.
Quick Start :
cat the raw.json to csv/xls use command line tool
cat raw.json | jsoncsv | mkexcel > output.csv
cat raw.json | jsoncsv | mkexcel -t xls > output.xls
make sure each line of raw json text file is a json object
$cat raw.json
{"id":1, "name":"A", "year": 2015}
{"id":2, "name":"S", "zone": "china"}
$cat raw.json | jsoncsv | mkexcel > output.csv
$cat output.csv
id,name,year,zone
1,A,2015,
2,S,,china
This is easily and needn’t care the different keys from any two object.
if input file is an json_array, use -A/–array to decode it
$cat raw.json
[{"id":1, "name":"A", "year": 2015}, {"id":2, "name":"S", "zone": "china"}]
$cat raw.json | jsoncsv -A | mkexcel > output.csv
$cat output.csv
id,name,year,zone
1,A,2015,
2,S,,china
another way to convert file step by step
$jsoncsv raw.json expand.json
$mkexcel expand.json -t xls output.xls
get more options with –help.
jsoncsv --help
mkexcel --help
Install
pip install jsoncsv
Usage
see #QuickStart and get more options with –help.
just expand/restore the json, the expand json is one layer json.
jsoncsv raw.json expand.json
jsoncsv -r expand.json raw.json
cat raw.json | jsoncsv | jsoncsv -r > raw2.json
mkexcel the expanded json (one layer)
mkexcel expand.json output.csv
mkexcel -t xls expand.json > output.xls
mkexcel -t csv expand.json > output.csv
-e, –expand
expand json, 展开 json
$jsoncsv -e raw.json expand.json
$cat raw.json expand.json
{"s":[1,2,{"w":1}]}
{"s.2.w": 1,"s.0": 1,"s.1": 2}
{“s”:[1,2,{“w”:1}]} will transformed to {“s.2.w”: 1,”s.0”: 1,”s.1”: 2}
the output “expand.json” is only one layer json, it can be easy change to csv or xlsx (with mkexcel)
-r, –restore
restore the expanded json 重构被展开的json
jsoncsv -r expand.json raw.json
cat expand.json raw.json
{"s.2.w": 1,"s.0": 1,"s.1": 2}
{"s": [1, 2, {"w": 1}]}
{“s.2.w”: 1,”s.0”: 1,”s.1”: 2} change to {“s”:[1,2,{“w”:1}]}
-s, –separator
separator used for combine the keys in the tree
default separator is .
–safe
on safe mode, use escape separator to avoid confilct
expand:
[‘aa’, ‘bb’, ‘www.xxx.com’] –> ‘aa.bb.www.xxx.com’
restore:
‘aa.bb.www.xxx.com’ –> [‘aa’, ‘bb’, ‘www.xxx.com’]
mkexcel
dump expanded (by jsoncsv) json file to csv or xls file
mkexcel expand.json output.csv
-t, –type
chose dump type in [‘csv’, ‘xls’] default is ‘csv’
cat expand.json|mkexcel -t csv > output.csv
cat expand.json|mkexcel -t xls > output.xls
NOTE/TODO
1. dict keys can’t be just array indexes
example:
echo '{"0":1,"1":[1,2]}'|jsoncsv -e| jsoncsv -r
[1, [1, 2]]
2. mkexcel enable hooks
wait next next version
3. unicodecsv is not good enough
but better than python strand library csv.
4. Windows is poor support
see https://github.com/alingse/jsoncsv/issues/37
try use https://jsoncsv.jsonutil.online/ instead
Metadata
Release files for jsoncsv 2.2.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| jsoncsv-2.2.5.tar.gz | 8.9 kB | Without provenance | Details |
Release files / jsoncsv-2.2.5.tar.gz
| Download URL | jsoncsv-2.2.5.tar.gz |
|---|---|
| Size | 8.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6286546debf4f784ca1b28d8dfc5b7d89bf96e226147c900179dc7d032456409
|
|
BLAKE2b-256 checksum How to use checksums |
70a9da2a843214104e7c451b4924fe062195ca40981686f2d382c19ca862adfd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.13.0 pkginfo/1.4.2 requests/2.19.1 setuptools/41.0.1 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/2.7.15
|
Security for jsoncsv 2.2.5
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.