Posts: 14 Threads: 9 Joined: Feb 2018 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() Posts: 12,117 Threads: 494 Joined: Sep 2016 Feb-27-2018, 10:54 PM (This post was last modified: Feb-27-2018, 10:55 PM by Larz60+.) 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) Posts: 14 Threads: 9 Joined: Feb 2018 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" Posts: 12,117 Threads: 494 Joined: Sep 2016 Feb-28-2018, 04:13 AM (This post was last modified: Feb-28-2018, 04:27 AM by Larz60+.) 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) Posts: 14 Threads: 9 Joined: Feb 2018 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. Posts: 2 Threads: 0 Joined: Oct 2025 Oct-14-2025, 08:36 AM (This post was last modified: Oct-15-2025, 02:10 AM by buran.) 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:04 AM:Spam content removed Posts: 1,210 Threads: 146 Joined: Jul 2017 Oct-15-2025, 12:05 AM (This post was last modified: Oct-15-2025, 12:05 AM by Pedroski55.) 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. |