jehiah / json2csv

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

Values from json array to seperate csv fields #17

Open RoelandSteur opened 10 years ago

RoelandSteur commented 10 years ago

I love this tool! I'm using it to convert json files from Twitter's streaming API to csv. I have very little programming skills, so this tool is really good for me.

However I'm stuck at this point where I would like to extract values from the coordinate pairs within tweets. I think the coordinates are stored as an array:

"coordinates": {"coordinates": [3.82955502, 51.48484907], "type": "Point"}

Would it be possible to store the X and Y-coordinate in a separate csv field?

I tried: "json2csv -k coordinates.coordinates[0],coordinates.coordinates[1] -i ...etc" but this doesn't work.

Thanks in advance! Roeland

jehiah commented 10 years ago

@RoelandSteur This is a great idea, and it's a feature set i hope json2csv grows to encompass.

There is another tool jq that has some more advanced json reformatting capabilities. You should be able to combine them to accomplish what you are looking for.

echo '{"coordinates": [3.82955502, 51.48484907], "type": "Point"}' | jq -c '{x:.coordinates[0],y:.coordinates[1]}' | json2csv -k x,y
3.829555,51.484849