JSON转CSV格式

https://pypi.org/project/python-json2csv/

Convert json array data to csv.

Note: zencore-json2csv rename to python-json2csv

1
pip install python-json2csv
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
E:\>json2csv --help
Usage: json2csv [OPTIONS]

Options:
-f, --file FILENAME     Input file name, use - for stdin.
--file-encoding TEXT    Input file encoding.
-o, --output FILENAME   Output file name, use - for stdout.
--output-encoding TEXT  Output file encoding.
-k, --keys TEXT         Output field names. Comma separated string list.
-p, --path TEXT         Path of the data.
--help                  Show this message and exit.

输入数据:

1
2
3
4
[
    [1,2,3],
    [2,3,4]
]

命令:

1
cat input.txt | json2csv -o output.txt

结果:

1
2
1,2,3
2,3,4

输入数据:

1
2
3
4
[
    {"f1": 11, "f2": 12, "f3": 13},
    {"f1": 21, "f3": 23, "f2": 22}
]

命令:

1
cat input.txt | json2csv -o output.txt -k f1,f2,f3

结果:

1
2
11,12,13
21,22,23

输入:

1
2
3
4
5
6
7
8
{
    "data": {
        "list": [
            [1,2,3],
            [2,3,4],
        ]
    }
}

命令:

1
cat input.txt | json2csv -o output.txt -p data.list

结果:

1
2
1,2,3
2,3,4