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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Convert Json to table format python_student 4 14,580 Dec-05-2024, 04:32 PM
Last Post: Larz60+
  Python Script to convert Json to CSV file chvsnarayana 8 4,731 Apr-26-2023, 10:31 PM
Last Post: DeaD_EyE
  Convert nested sample json api data into csv in python shantanu97 3 5,291 May-21-2022, 01:30 PM
Last Post: deanhystad
  Convert python dataframe to nested json kat417 1 7,863 Mar-18-2022, 09:14 PM
Last Post: kat417
Question convert unlabeled list of tuples to json (string) masterAndreas 4 9,197 Apr-27-2021, 10:35 AM
Last Post: masterAndreas
  Convert string to JSON using a for loop PG_Breizh 3 3,733 Jan-08-2021, 06:10 PM
Last Post: PG_Breizh
  How to convert what appears to be a JSON file to CSV NewBeie 4 3,370 Aug-28-2020, 04:45 PM
Last Post: Larz60+
  convert a json file to a python dictionnary of array Reims 2 2,969 Sep-10-2019, 01:08 PM
Last Post: Reims
  Python convert csv to json with nested array without pandas terrydidi 2 11,262 Jan-12-2019, 02:25 AM
Last Post: terrydidi
  Issue with a script to convert xls to json Will86 2 4,512 Dec-19-2018, 08:23 AM
Last Post: Will86

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020