0

I am fairly new to Python and have several nested JSON files I need to convert to CSV's.

The structure of these is the following:

{'Z': {'@SchemaVersion': 9,
  'FD': [{'FDRecord': [{'NewCase': {'@TdrRecVer': 5,
   'CaseLabel': '',
   'StdHdr': {'DevDateTime': '2000-05-02T10:43:18',
    'ElapsedTime': 0,
    'GUID': '5815D34615C15690936B822714009468',
    'MsecTime': 5012,
    'RecId': 4},
   'UniqueCaseId': '5389F346136315497325122714009468'}},
 {'NewCase': {'@TdrRecVer': 5,
   'CaseLabel': '',
   'StdHdr': {'DevDateTime': '2000-05-02T10:43:18',
    'ElapsedTime': 0,
    'GUID': '5819D346166610458312622714009468',
    'MsecTime': 9459,
    'RecId': 4},
   'UniqueCaseId': '5819F346148627009653284714009468'}},
 {'AnnotationEvt': {'@EvtName': 'New',
   '@TdrRecVer': 1,
   'DevEvtCode': 13,
   'Payload': '0 0 0 0',
   'StdHdr': {'DevDateTime': '2000-05-02T10:43:18',
    'ElapsedTime': 0,
    'GUID': '5899D34616BC1000938B824538219968',
    'MsecTime': 7853,
    'RecId': 8},
   'TreatmentSummary': 1,
   'XidCode': '0000000B'}},
 {'TrendRpt': {'@TdrRecVer': 9,
   'CntId': 0,
   'DevEvtCode': 30,
   'StdHdr': {'DevDateTime': '2000-05-02T10:43:18',
    'ElapsedTime': 0,
    'GUID': '5819C34616781004852225698409468',
    'MsecTime': 4052,
    'RecId': 12}, ...

My problem is, most examples online show how to read in a very small json and write it out to a csv by explicitly stating the keys or field names when creating a csv. My files are far too large to do this, some of them being over 40MB.

I tried following another person's example (below) from online, but did not succeed:

import json
import csv

with open('path-where-json-located') as file:
    data_parse = json.load(file)

data = data_parse['Z']

data_writer = open('path-for-csv', 'w')

csvwriter = csv.writer(data_writer)

count=0
for i in data:
    if count == 0:
        header = i.keys()
        csvwriter.writerow(header)
        count+=1
    csvwriter.writerow(i.values())

data_writer.close()

When I run this, I get the following error:

AttributeError: 'str' object has no attribute 'keys'

I understand that for some reason it is treating the key I want to pull as a string object, but I do not know how to get around this and correctly parse this into a csv.

3
  • 3
    data.keys() not i.keys(). But the problem is: your structure is not flat. How can you write all the fields in a row? even if you could it wouldn't make sense. Commented Dec 8, 2016 at 20:34
  • 2
    what do you think the first few rows of your output should look like? Maybe csv isn't the best structure for you Commented Dec 8, 2016 at 20:38
  • I've found it useful, when trying to figure out how to map JSON data to a csv, to print the data out in a nice readable format using print(json.dumps(data, indent=4)) (assuming data contains the contents of a JSON file).
    – martineau
    Commented Dec 8, 2016 at 22:06

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.