evidens / json2csv

Converts JSON files to CSV (pulling data from nested structures). Useful for Mongo data
MIT License
263 stars 98 forks source link

Nested Json to csv in python 3 #38

Open ravikuc opened 6 years ago

ravikuc commented 6 years ago

I am trying to convert JSON data into a CSV in Python3, but it no longer works with this script, giving me different errors. Anyone know how to fix for Python 3? Thanks.

Below is my JSON data:

{ "fruit": [ { "name": "Apple", "binomial name": "Malus domestica", "major_producers": [ "China", "United States", "Turkey" ], "nutrition": { "carbohydrates": "13.81g", "fat": "0.17g", "protein": "0.26g" } }, { "name": "Orange", "binomial name": "Citrus x sinensis", "major_producers": [ "Brazil", "United States", "India" ], "nutrition": { "carbohydrates": "11.75g", "fat": "0.12g", "protein": "0.94g" } }, { "name": "Mango", "binomial name": "Mangifera indica", "major_producers": [ "India", "China", "Thailand" ], "nutrition": { "carbohydrates": "15g", "fat": "0.38g", "protein": "0.82g" } } ] }

ravikuc commented 6 years ago

The output CSV should look like: capture

JeffMv commented 4 years ago

Hi @ravikuc (or rather anyone looking to make it work for python 3), I created a fork for Python 3 and added most of the changes made up until today (2020-06) by other forks. I dropped python 2.

If you want to do it by yourself, just the best thing to do would be to execute the python command utility 2to3 with option -w to convert the deprecated Python 2 code to Python 3. For instance on Unix:

2to3 -w json2csv.py
bitscott1102 commented 4 years ago

@ravikuc you can flatten json by from flatten_json import flatten. Then apply json2csv

brahma19 commented 3 years ago

@ravikuc refer to py3 port of the same, https://github.com/brahma19/json2csvpy3

I have tested this in my local and getting this as output

image