0

I have .json file which have this type of data ,the name of universities of the world

[
  {
    "web_pages": [
      "https://www.cstj.qc.ca",
      "https://ccmt.cstj.qc.ca",
      "https://ccml.cstj.qc.ca"
    ],
    "name": "Cégep de Saint-Jérôme",
    "alpha_two_code": "CA",
    "state-province": null,
    "domains": [
      "cstj.qc.ca"
    ],
    "country": "Canada"
  },
  {
    "web_pages": [
      "http://www.lindenwood.edu/"
    ],
    "name": "Lindenwood University",
    "alpha_two_code": "US",
    "state-province": null,
    "domains": [
      "lindenwood.edu"
    ],
    "country": "United States"
  },
  {
    "web_pages": [
.......
.....
....
...
Continue......

I want to convert this .json file into CSV using Python, What will be its solution to make the CSV file?

7
  • First of all, I'd advise you to try parsing the JSON with a different encoding - your current one seems to be generating all sorts of weird characters. Commented Mar 6, 2019 at 15:32
  • Do you have Pandas, or you want to do this with only core Python? Commented Mar 6, 2019 at 15:36
  • @SupratimHaldar I am using simple IDL python 3.6 ,Install it and then I am using it. Commented Mar 6, 2019 at 15:37
  • Okay. I am adding this as a comment rather than an answer because this solution involves Pandas. import json from pandas.io.json import json_normalize with open('infile.json') as json_data: d = json.load(json_data) df = json_normalize(d) df.to_csv('outfile.csv', index=False) Commented Mar 6, 2019 at 15:51
  • @SupratimHaldar I have Install the pandas ,Can you Give me as a Answer Commented Mar 6, 2019 at 16:06

1 Answer 1

1

This solution uses Pandas.

import json
from pandas.io.json import json_normalize

with open('infile.json') as json_data:
    d = json.load(json_data)

df = json_normalize(d)
df.to_csv('outfile.csv', index=False)

Also, as @LucaBezerra has mentioned in the comments, the current text has some encoding problem which you might want to fix (look at the first "name").

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.