Python Forum
JSON File - extract only the data in a nested array for CSV file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
JSON File - extract only the data in a nested array for CSV file
#1
Trying to get the data in a nested array "action_history" from a JSON file. Tried normalizing, but hit a wall.

JSON file looks like this:
{
"783": {        
        "recordID": 783,
        "title": "Test1",
        "date": 1723572991,        
        "action_history": [
            {
                "recordID": 783,               
                "time": 1723573283,                
                "actionType": "submit"               
            },           
            {
                "recordID": 783,               
                "time": 1723573425,               
                "actionType": "Save"
            },
            {
                "recordID": 783,               
                "time": 1723575689,               
                "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"
            }
        ]       
    }
}
My current code:
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", "title", "date",
                  "action_time",
                  "action_history"
                  ]
        f.writerow(header)
        for x in data.values():
            result = [x["recordID"],
                      x["title"],
                      x["date"],
                      x["action_history"]
                      ]

            f.writerow(result)


# src, dest, function call
json_file = 'C:\\Users\\VHATUCStoehD\\Desktop\\Simple.json'
csv_file = 'C:\\Users\\VHATUCStoehD\\Desktop\\PY_output.csv'
json_to_csv(json_file, csv_file)
Current output:
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:
Output:
recordID,actionType,time 783,"submit",1723573283 783,"Save",1723573425 783,"Save",1723575689 783,"Complete",1723585061 900,"submit",1723573300 900,"Save",1723573350 900,"Complete",1723585390
Reply
#2
Something like this should work.
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)
shwfgd likes this post
Reply
#3
Beautiful! Thank you!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to write variable in a python file then import it in another python file? tatahuft 4 946 Jan-01-2025, 12:18 AM
Last Post: Skaperen
  Why can't it extract the data from .txt well? Melcu54 4 1,780 Dec-12-2024, 07:36 PM
Last Post: Melcu54
  Write json data to csv Olive 6 1,345 Oct-22-2024, 06:59 AM
Last Post: Olive
  Python script to extract data from API to database melpys 0 865 Aug-12-2024, 05:53 PM
Last Post: melpys
  FileNotFoundError: [Errno 2] No such file or directory although the file exists Arnibandyo 0 958 Aug-12-2024, 09:11 AM
Last Post: Arnibandyo
  To fetch and iterate data from CSV file using python vyom1109 3 1,024 Aug-05-2024, 10:05 AM
Last Post: Pedroski55
  Extract and rename a file from an Archive tester_V 4 3,724 Jul-08-2024, 07:54 AM
Last Post: tester_V
  "[Errno 2] No such file or directory" (.py file) IbrahimBennani 13 6,417 Jun-17-2024, 12:26 AM
Last Post: AdamHensley
  Reading an ASCII text file and parsing data... oradba4u 2 1,436 Jun-08-2024, 12:41 AM
Last Post: oradba4u
  Extracting the correct data from a CSV file S2G 6 1,820 Jun-03-2024, 04:50 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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