jehiah / json2csv

command line tool to convert json to csv
MIT License
801 stars 94 forks source link

output is putting all results into a single row #37

Closed lsantosdan closed 4 years ago

lsantosdan commented 5 years ago

I'm trying to parse through Instagram follower data, structured as such:

{
    close_friends: { 
        username: timestamp
    }
    followers: {
        follower1: timestamp1, 
        followerN: timestampN
    }
}

Using the command ./json2csv -i input.json -k followers -o output.csv outputs all followers+timestamp in a single row/cell. Is there a way to output 1 follower per row?

Thank you.

grocky commented 5 years ago

@lsantosdan I think you need to do some transformation on the json before you can convert it. With the command you listed above you're asking json2csv to print the object into the followers column. json2csv expects the data in JSONL format (basically every line should be a json object).

So, using the example you provided:

instagram_sample.json

{
    "close_friends": { 
        "username": "timestamp"
    },
    "followers": {
        "follower1": "timestamp1", 
        "followerN": "timestampN"
    }
}

you can convert it to a JSONL file like this:

< instagram_sample.json jq -c '.followers | to_entries[] | { follower_name: .key, timestamp: .value }' > followers.json

Then, you can finally convert the followers to csv like this:

< followers.json json2csv -p -k follower_name,timestamp

which outputs the data as:

follower_name,timestamp
follower1,timestamp1
followerN,timestampN

I got a lot of these ideas from this handy book: https://www.datascienceatthecommandline.com/chapter-5-scrubbing-data.html#working-with-xmlhtml-and-json