Ruel Nopal
Ruel Nopal

Reputation: 415

Error while converting json to csv using csvkit

Hi i downloaded a csvkit to my macbook everything went well by following the tutorial enter link description here however when i tried to convert the json files using this command

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

This is the error

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

I dont know what to put the or where to put the key as spcified by the error heres my json files

{

"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"
    }]}

Upvotes: 2

Views: 2912

Answers (3)

Ruel Nopal
Ruel Nopal

Reputation: 415

Ive got the answer heres the code

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

where items is the KEY

Upvotes: 2

jDo
jDo

Reputation: 4010

Calling the program like so:

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

Produces:

$ 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

Upvotes: 1

You should specify the key under which there is a list objects to make into the rows of CSV. In this particular case, items:

in2csv -k items test.json

As a general hint, if you run a command without arguments, you will get short usage, and longer with --help switch:

% 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).

and

% 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.

Upvotes: 3

Related Questions