0

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"
    }]}
Ruel Nopal
  • 385
  • 3
  • 15
  • Did you check [this](https://github.com/wireservice/csvkit/issues/361) already? (concerns the error you're seeing) – jDo Mar 10 '16 at 15:13
  • yeah i did it does not say anyting there where to put the key – Ruel Nopal Mar 10 '16 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 – Ruel Nopal Mar 10 '16 at 15:21

3 Answers3

2

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.
  • 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` – Ruel Nopal Mar 10 '16 at 15:18
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
jDo
  • 3,962
  • 1
  • 11
  • 30
1

Ive got the answer heres the code

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

where items is the KEY

Ruel Nopal
  • 385
  • 3
  • 15