json2csv icon indicating copy to clipboard operation
json2csv copied to clipboard

Nested Json to csv in python 3

Open ravikuc opened this issue 7 years ago • 4 comments

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 avatar May 24 '18 05:05 ravikuc

The output CSV should look like: capture

ravikuc avatar May 24 '18 05:05 ravikuc

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

JeffMv avatar Jun 29 '20 17:06 JeffMv

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

bitscott1102 avatar Sep 18 '20 23:09 bitscott1102

@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

brahma19 avatar Jun 26 '21 18:06 brahma19