0

嗨,我下载了一个 csvkit 到我的 macbook,按照教程在此处输入链接描述一切顺利,但是当我尝试使用此命令转换 json 文件时

in2csv customers-page-1.json > customers-page-1.csv

这是错误

When converting a JSON document with a top-level dictionary element, a key must be specified.

我不知道该放什么或在哪里放置错误指定的密钥,这是我的 json 文件

{

"page":1,
"pages":132,
"count":5945,
"items":[
    {
        "id":74798241,
        "firstName":"Edyth",
        "lastName":"U.",
        "fullName":"Edyth U.",
        "photoUrl":null,
        "photoType":null,
        "gender":"unknown",
        "age":null,
        "organization":null,
        "jobTitle":null,
        "location":null,
        "createdAt":"2016-03-02T04:26:52Z",
        "modifiedAt":"2016-03-02T04:26:52Z"
    }]}
4

3 回答 3

2

您应该指定要在 CSV 行中列出对象的键。在这种特殊情况下,items

in2csv -k items test.json

作为一般提示,如果您运行不带参数的命令,则使用时间会很短,而使用--helpswitch 会更长:

% in2csv
usage: in2csv [-h] [-d DELIMITER] [-t] [-q QUOTECHAR] [-u {0,1,2,3}] [-b]
              [-p ESCAPECHAR] [-z MAXFIELDSIZE] [-e ENCODING] [-S] [-H] [-v]
              [-l] [--zero] [-f FILETYPE] [-s SCHEMA] [-k KEY] [-y SNIFFLIMIT]
              [--sheet SHEET] [--no-inference]
              [FILE]
in2csv: error: You must specify a format when providing data via STDIN (pipe).

% in2csv --help
usage: in2csv [-h] [-d DELIMITER] [-t] [-q QUOTECHAR] [-u {0,1,2,3}] [-b]
              [-p ESCAPECHAR] [-z MAXFIELDSIZE] [-e ENCODING] [-S] [-H] [-v]
              [-l] [--zero] [-f FILETYPE] [-s SCHEMA] [-k KEY] [-y SNIFFLIMIT]
              [--sheet SHEET] [--no-inference]
              [FILE]

Convert common, but less awesome, tabular data formats to CSV.
....
  -k KEY, --key KEY     Specifies a top-level key to use look within for a
                        list of objects to be converted when processing JSON.
于 2016-03-10T15:07:46.313 回答
1

像这样调用程序:

$ in2csv --key items customers-page-1.json > customer.csv

生产:

$ cat customer.csv 
id,firstName,lastName,fullName,photoUrl,photoType,gender,age,organization,jobTitle,location,createdAt,modifiedAt
74798241,Edyth,U.,Edyth U.,,,unknown,,,,,2016-03-02T04:26:52Z,2016-03-02T04:26:52Z
于 2016-03-10T15:25:14.647 回答
1

我得到了答案,代码如下

in2csv -k items customers-page-1.json > customers-page-1.csv

其中itemsKEY

于 2016-03-10T15:29:25.490 回答