2

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"
    }]}
3
  • Did you check this already? (concerns the error you're seeing) Commented Mar 10, 2016 at 15:13
  • yeah i did it does not say anyting there where to put the key Commented Mar 10, 2016 at 15:17
  • by the way i could easily convert it with online json converter but i have thousand of json files it takes me forever to convert if i have to do it there Commented Mar 10, 2016 at 15:21

3 Answers 3

3

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.
Sign up to request clarification or add additional context in comments.

1 Comment

yes i know thats the key however i dont know where to put that key inside this command in2csv customers-page-1.json > customers-page-1.csv
2

Ive got the answer heres the code

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

where items is the KEY

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.