Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert JSON to CSV
#1
I have been searching google and working on converting a JSON to a CSV but have not been successful. I call an API from newsapi.org and I can get the json file but when I run the code I get the news1.csv but when I open it, it is blank. I am using Python 2.7.

import json import csv with open('news1.json', 'r') as f: dicts = json.load(f) out = open('news1.csv', 'w') writer = csv.DictWriter(out, dicts[0].keys()) writer.writeheader() writer.writerows(dicts) out.close()
Reply
#2
This is untested, and won't work with nested dict (it can, but to do so need sample json file)
It is also untested, but should be close if not correct:
dlist = [] for key, value in dicts.iteritems(): dlist.append([key, value]) writer.writerows(dlist)
Reply
#3
I ran the code and received an error: UniCodeDecodeError: "charmap" codec can't decode byte 0x9d in position 7587: character maps to (undefined). I tried the encoding="uft8", but that did not help. Here is a sample of my JSON file. Thank you for the help.

{"status":"ok","totalResults":20,"articles":[{"source":{"id":null,"name":"Bbc.com"},"author":"https://www.facebook.com/bbcnews","title":"Takata airbag scandal: Australia recalls 2.3 million cars","description":"Millions of vehicles have faulty Takata airbags, forcing one the nation's biggest consumer recalls.","url":"http://www.bbc.com/news/world-australia-43220640","urlToImage":"https://ichef.bbci.co.uk/news/1024/branded_news/9D10/production/_96680204_mediaitem96680203.jpg","publishedAt":"2018-02-28T00:34:26Z"},{"source":{"id":"the-wall-street-journal","name":"The Wall Street Journal"},"author"
Reply
#4
My suggestion would be to upgrade to python 3.
Python 2 will no longer be supported in about 1.5 years.
but let me have a go at it. I still have python 2.7 around here somewhere.
could you please attach the json file
doesn't work as posted (JSON.parse: expected ':' after property name in object at line 1 column 603 of the JSON data)
Reply
#5
Thank you for your help so far. I really appreciate it. I do have Python3 so I will try it with 3. I cannot upload an attachment yet, I am a new user so I am restricted. If it helps I got the information from newsapi.org. I am new at using API's so I was working on this to see how it goes. I will try Python 3 and let you know. Again, really like this forum very friendly people. Have a great day.
Reply
#6
I know it was really a tricky task to convert JSON to CSV. On the internet, a couple of techniques and methods are available to convert a JSON file to CSV. So, below I mentioned a method to convert JSON files.

Method 1: Convert JSON files to CSV | Using Excel
Open Excel
  • Go to the Data tab → click Get Data → From File → From JSON.
    (If you’re on an older version, it might be under Get & Transform Data.)

    Select your JSON file
    Choose your .json file from your computer.
    Excel will open the Power Query Editor window showing “Record” or “List” entries — that’s your JSON structure.

    Expand the JSON data
    Click the small expand icon (↔) next to each column to open up nested data.
    Keep expanding until you see all the fields you want — for example, title, description, contact. value, etc.

    Flatten nested lists or arrays.
    If your JSON contains lists (like multiple contacts or tags), you can expand those into separate rows or columns using the same expand icon.

    Load data into Excel
    Once the data looks like a normal table, click Close & Load.
    Excel will import it into a new worksheet.

    Save as CSV
    Go to File → Save As → Browse → Save as type: CSV (Comma delimited).
    Choose where to save it and you’re done!

    By using the above method, you can convert your small JSON files into CSV format, but when you have large JSON files, it is a complex and risky task.

    I hope this was helpful to you!
buran write Oct-15-2025, 02:10 AM:
Please, note this is a python forum and OP is looking for programmatic solution using python. Posting AI generated content isn't helpful
buran write Oct-15-2025, 02:04 AM:
Spam content removed
Reply
#7
Exactly how you do this depends a little on the inner structure of your json file, but it is easy.

This is a nested dictionary from another thread here saved as mydata.json. I put it here so you can see the structure.

Quote:products = {
"one": {"name": "Laptop", "price": {"amount": 999, "currency": "USD"}},
"two": {"name": "Smartphone", "price": {"amount": 499, "currency": "USD"}},
"three":{"name": "Tablet", "price": {"amount": 299, "currency": "EUR"}},
"four": {"name": "Monitor", "price": {"amount": 199, "currency": "USD"}},
"five": {"name": "Camera", "price": {"amount": 599, "currency": "EUR"}},
"six": {"name": "Cat Camera", "price": {"amount": 699, "currency": "EUR"}},
"seven": {"name": "Baby Monitor", "price": {"amount": 399, "currency": "USD"}},
"eight": {"name": "Laptop dancer", "price": {"amount": 9999, "currency": "USD"}},
"nine": {"name": "Dumbphone", "price": {"amount": 1, "currency": "RMB"}},
"ten": {"name": "Poison Tablet", "price": {"amount": 1555, "currency": "EUR"}},
}

Load the above using Python json module, the rest is easy.

import json import csv myjson = '/home/peterr/temp/mydata.json' with open(myjson, 'r', encoding='utf-8') as infile: data = json.load(infile) type(data) # returns <class 'dict'> # you need to determine the column names from the structure of your particular json headers = ['id', 'name', 'amount', 'currency'] # csv.writer() wants the rows as lists rows = [] for key in data.keys(): row = [key, data[key]['name'], data[key]['price']['amount'], data[key]['price']['currency']] rows.append(row) savename = '/home/peterr/temp/products.csv' with open(savename, 'w') as csvfile: csvwriter = csv.writer(csvfile) csvwriter.writerow(headers) csvwriter.writerows(rows)
Maybe the module csv has a built in method to do this, but I like to know how things work, if possible!

You can post a few lines of your json file as text here, if you still have trouble. You don't have to post the whole file.

If your json file is very large, we can think of strategies to not overload memory.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Convert Json to table formathttps://python-forum.io/thread-38313.html python_student 3 18,490 Dec-05-2024, 04:32 PM
Last Post: Larz60+
  Python Script to convert Json to CSV file chvsnarayana 8 6,297 Apr-26-2023, 10:31 PM
Last Post: DeaD_EyE
  Convert nested sample json api data into csv in python shantanu97 3 6,930 May-21-2022, 01:30 PM
Last Post: deanhystad
  Convert python dataframe to nested json kat417 1 8,710 Mar-18-2022, 09:14 PM
Last Post: kat417
Question convert unlabeled list of tuples to json (string) masterAndreas 4 10,390 Apr-27-2021, 10:35 AM
Last Post: masterAndreas
  Convert string to JSON using a for loop PG_Breizh 3 4,468 Jan-08-2021, 06:10 PM
Last Post: PG_Breizh
  How to convert what appears to be a JSON file to CSV NewBeie 4 4,049 Aug-28-2020, 04:45 PM
Last Post: Larz60+
  convert a json file to a python dictionnary of array Reims 2 3,529 Sep-10-2019, 01:08 PM
Last Post: Reims
  Python convert csv to json with nested array without pandas terrydidi 2 12,434 Jan-12-2019, 02:25 AM
Last Post: terrydidi
  Issue with a script to convert xls to json Will86 2 5,016 Dec-19-2018, 08:23 AM
Last Post: Will86

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.