Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested Json to csv in python 3 #38

Open
ravikuc opened this issue May 24, 2018 · 4 comments
Open

Nested Json to csv in python 3 #38

ravikuc opened this issue May 24, 2018 · 4 comments

Comments

@ravikuc
Copy link

ravikuc commented May 24, 2018

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
Copy link
Author

ravikuc commented May 24, 2018

The output CSV should look like:
capture

@JeffMv
Copy link

JeffMv commented Jun 29, 2020

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
Copy link

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

@brahma19
Copy link

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants