Python Forum
Python Script to convert Json to CSV file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Script to convert Json to CSV file
#1
Hi All,

We have one requirement to convert Json to CSV file. In json file, values are there for all the columns, some columns are missing but when data populated in CSV file for those values missing columns should have blank/null, next column values should not overwrite.

Json Sample data:
{"results":[{"fruit": "Apple","size": "Large","color": "Red"},{"fruit": "Banana","color": "Yellow"},{"fruit": "Watermelon","size": "Large"},{"fruit": "Orange","color": "Orange"}]}

Expected format:
Fruit,Size,Color
Apple,Large,Red
Banana,,Yellow
Watermelon,Large,
Orange,,Orange

Require your help in script preparation. Whether we can handle all the columns/Headings while reading data from json file or do we need to handle all the columns/headings while populating data in csv file...Please advise

Thanks in advance
Reply
#2
Show what you tried and what error message you got.
Reply
#3
Do you know all fields in advance or do you need to handle them dynamically?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
Can you use pandas? Using pandas this is a few lines of code.

I don't understand this:
Output:
Whether we can handle all the columns/Headings while reading data from json file
You don't read a json file, you load it. The json.load(file) command reads the entire json and returns Python objects. In this case it returns a dictionary containing a list of dictionaries. You'll have to scan through the list to find all the keys. The keys will be the column headings for your csv file. Then you'll have to unpack the dictionaries into some kind of table, or maybe just write directly to the csv file. Either way you'll have to provide a filler when a dictionary does not have all the heading keys.
Reply
#5
You should use first csv.


import csv
import io

results = {
    "results": [
        {"fruit": "Apple", "size": "Large", "color": "Red"},
        {"fruit": "Banana", "color": "Yellow"},
        {"fruit": "Watermelon", "size": "Large"},
        {"fruit": "Orange", "color": "Orange"},
    ]
}

fields = tuple(results["results"][0])

with io.StringIO() as fake_file:
    writer = csv.DictWriter(fake_file, fieldnames=fields)
    writer.writeheader()
    for row in results["results"]:
        writer.writerow(row)

    print(fake_file.getvalue())
The shorter version uses the method writerows, where the for-loop is inside the csv-module:
import csv
import io

results = {
    "results": [
        {"fruit": "Apple", "size": "Large", "color": "Red"},
        {"fruit": "Banana", "color": "Yellow"},
        {"fruit": "Watermelon", "size": "Large"},
        {"fruit": "Orange", "color": "Orange"},
    ]
}

fields = tuple(results["results"][0])
with io.StringIO() as fake_file:
    writer = csv.DictWriter(fake_file, fieldnames=fields)
    writer.writeheader()
    writer.writerows(results["results"])

    print(fake_file.getvalue())
Output:
Output:
fruit,size,color Apple,Large,Red Banana,,Yellow Watermelon,Large, Orange,,Orange
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#6
(Apr-26-2023, 07:39 AM)DeaD_EyE Wrote: fields = tuple(results["results"][0])
Note, it could be just coincidence that first element has all possible keys. That's why I asked if OP knows full list of field names in advance. Otherwise they need to loop over all dicts and combine keys. In this case it's also important to clarify if the order of the field names in the csv matters
DeaD_EyE likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
If each data point has different keys, you can collect them:
#modified example data

results = {
    "results": [
        {"fruit": "Apple", "size": "Large", "color": "Red"},
        {"fruit": "Banana", "color": "Yellow"},
        {"fruit": "Watermelon", "size": "Large", "humidity": 0.9},
        {"fruit": "Orange", "color": "Orange", "diameter": 33.5},
    ]
}

# getting all keys from all results and keeping the insertion order

fields = tuple(dict.fromkeys([key for row in results["results"] for key in row]))
The nested list comprehension as nested for-loops:
fields = {}
for row in results["results"]:
    for key in row:
        print(f"Adding {key} to fields.")
        fields[key] = None
    print("Next row", end="\n\n")

print()
print("Fields before it's converted to a list or tuple")
print(fields)
print()
print("After conversion to a tuple")
fields = tuple(fields)
print(fields)
If you don't care about order, you could use a set instead. If you want to sort the fields, it's also possible.
sorted_fields = sorted(set(key for result in results["results"] for key in result.keys()))
Or as a for-loop:
fields = set()
for result in results["results"]:
    for key in result:
        fields.add(key)
fields = sorted(fields)
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#8
Though this is not posted in the Homework Forum, it is obviously a homework question.
Reply
#9
Oh... the good part is, that it's not the complete solution.
Reading and writing is not included.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  encrypt data in json file help jacksfrustration 1 230 Mar-28-2024, 05:16 PM
Last Post: deanhystad
Question [SOLVED] Correct way to convert file from cp-1252 to utf-8? Winfried 8 905 Feb-29-2024, 12:30 AM
Last Post: Winfried
  parse json field from csv file lebossejames 4 768 Nov-14-2023, 11:34 PM
Last Post: snippsat
  Convert File to Data URL michaelnicol 3 1,180 Jul-08-2023, 11:35 AM
Last Post: DeaD_EyE
  Is there a *.bat DOS batch script to *.py Python Script converter? pstein 3 3,284 Jun-29-2023, 11:57 AM
Last Post: gologica
  Loop through json file and reset values [SOLVED] AlphaInc 2 2,150 Apr-06-2023, 11:15 AM
Last Post: AlphaInc
  Converting a json file to a dataframe with rows and columns eyavuz21 13 4,522 Jan-29-2023, 03:59 PM
Last Post: eyavuz21
  validate large json file with millions of records in batches herobpv 3 1,283 Dec-10-2022, 10:36 PM
Last Post: bowlofred
  Convert Excel file into csv with Pipe symbol.. mg24 4 1,340 Oct-18-2022, 02:59 PM
Last Post: Larz60+
  Convert Json to table format python_student 2 5,570 Sep-28-2022, 12:48 PM
Last Post: python_student

Forum Jump:

User Panel Messages

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