pythonjsoncsv

Convert JSON dictionary file to CSV


Need to extract recordID, date, title, and breakout action_history data. Since the action_history data also has the recordID, it does not all have to be on the same row for the CSV file. Here's the JSON string:

{
"783": {        
        "recordID": 783,
        "title": "Test1",
        "date": 1723572991,        
        "action_history": [
            {
                "recordID": 783,               
                "time": 1723573283,                
                "actionType": "submit"               
            },           
            {
                "recordID": 783,               
                "time": 1723573425,               
                "actionType": "Save"
            },
            {
                "recordID": 783,               
                "time": 1723585061,
                "actionType": "Complete"
            }
        ]       
    },
"900": {        
        "recordID": 900,
        "title": "Test2",
        "date": 1723572825,        
        "action_history": [
            {
                "recordID": 900,               
                "time": 1723573300,                
                "actionType": "submit"               
            },           
            {
                "recordID": 900,               
                "time": 1723573350,               
                "actionType": "Save"
            },
            {
                "recordID": 900,               
                "time": 1723585390,
                "actionType": "Complete"
            }
        ]       
    }
}

Not getting very far with just the basic json.load and csv conversion methods. With this JSON dictionary structure (not an array), I'm having trouble breaking out the action_history for each recordID. The action_history can be in different rows since the recordID is indicated (I can join the records later).

File output:

recordID,title,date,action_history
783,Test1,1723572991,"[{'recordID': 783, 'time': 1723573283, 'actionType': 'submit'}, {'recordID': 783, 'time': 1723573425, 'actionType': 'Save'}, {'recordID': 783, 'time': 1723585061, 'actionType': 'Complete'}]"
900,Test2,1723572825,"[{'recordID': 900, 'time': 1723573300, 'actionType': 'submit'}, {'recordID': 900, 'time': 1723573350, 'actionType': 'Save'}, {'recordID': 900, 'time': 1723585390, 'actionType': 'Complete'}]"

Preferred output:
recordID,title,date,actionType,time
783,Test1,1723572991,"submit",1723573283
783,Test1,1723572991,"Save",1723573425
783,Test1,1723572991,"Complete",1723585061
900,Test2,1723572825,"submit",1723573300
900,Test2,1723572825,"Save",1723573350
900,Test2,1723572825,"Complete",1723585390

Script:

import csv
import json

def json_to_csv(json_file, csv_file):

    with open(json_file) as f:
        data = json.load(f)

        f = csv.writer(open(csv_file, "w+", newline=""))

        f.writerow(["recordID","title","date","action_history"])
        for x in data.values():
            f.writerow([x["recordID"],
                        x["title"],
                        x["date"],
                        x["action_history"]])

#src, dest, function call
json_file = 'source.json'
csv_file = 'output.csv'
json_to_csv(json_file, csv_file)

Solution

  • The following code was provided by another user "snippsat" on python-forum.io:

    import csv
    import json
     
    def json_to_csv(json_file, csv_file):
        with open(json_file) as f:
            data = json.load(f)
            f = csv.writer(open(csv_file, "w+", newline=""))
            header = ["recordID", "actionType", "time"]
            f.writerow(header)
            # Iterate through each record in the JSON data
            for record in data.values():
                record_id = record["recordID"]
                for action in record["action_history"]:
                    f.writerow([record_id, action["actionType"], action["time"]])
     
    json_file = 'Simple.json'
    csv_file = 'output.csv'
    json_to_csv(json_file, csv_file)