5

In python I have a complex object hierarchy made of lists and dictionaries. I want to spit it all out to CSV or some other kind of database format. Any answers in Python or Javascript very much appreciated.

I understand that one CSV file (or table) can only represent one 'level' of object in my hierarchy, so the solution would need to create multiple files.

Here is an example:

{
    "Person" : [{"name":"Greg","age":"35","car":["honda civic","ford focus"]},
                {"name":"Steve","age":"28", "car":["mazda 323", "toyota camry"]}]
}

would become

Person.csv:
id,name,age
1,Greg,35
2,Steve,28

car.csv:
id,Person_id,value
1,1,honda civic
2,1,ford focus
3,2,mazda 323
4,2,toyota camry

Basically the only thing interesting going on here is the assignment of new ids so that the rows in the tables can be associated.

Cheers, Dave

4 Answers 4

2

try something like this.

json_dict = {
    "Person" : [{"name":"Greg","age":"35","car":["honda civic","ford focus"]},
                {"name":"Steve","age":"28", "car":["mazda 323", "toyota camry"]}]
}

for entity in json_dict:
    csv_file = open('%s.csv' % entity, 'wb')
    headers = a[entity][0].keys()
    csv_writer = csv.DictWriter(csv_file, headers)
    map(csv_writer.writerow, json_dict[entity])
    csv_file.close()

# Now you have your json to csv file for formatting you can use awk;

awk  -F , '{print NR ", " $2 ", " $3 }' Person.csv > person.csv

...

0

assignment of new ids so that the rows in the tables can be associated.

As in:

  1. Create a Primary Key (PK) for each row.

  2. Create a Foreign Key (FK) relationship between Car and Person. It appears that Car has a "dependent" relationship on Person.

Step 1. Use enumerate on your JSON objects. This will give you a handy PK for each Person.

Step 2. Use the handy PK for each Person as the FK for each Car you create.

The only thing that's unpleasant is assigning PK's to the Cars since there's no handy way to use enumerate on your particular data structure. For that, you have to use a good old counter.

http://docs.python.org/library/functions.html#enumerate

0
import json
import csv
temp = json.load(open('filename.json','r'))
output =[]
for each in temp:
     row = {}
     row['field1'] =each['field1']
     row['field2'] = each['field2']
     output.append(row)
file = open( "filename_destination.csv", "w")

fileWriter = csv.writer(file , delimiter=",",quotechar='"', quoting=csv.QUOTE_MINIMAL)

Header = ['field1','field2']

fileWriter.writerow(Header)

for x in output:
te = [x['field1'],x['field2']]
fileWriter.writerow(te)
file.close()
0

I just released a module that makes this process easy in Node.js

var jsonexport = require('jsonexport');

var contacts = [{
   name: 'Bob',
   lastname: 'Smith',
   family: {
       name: 'Peter',
       type: 'Father'
   }
},{
   name: 'James',
   lastname: 'David',
   family:{
       name: 'Julie',
       type: 'Mother'
   }
},{
   name: 'Robert',
   lastname: 'Miller',
   family: null,
   location: [1231,3214,4214]
},{
   name: 'David',
   lastname: 'Martin',
   nickname: 'dmartin'
}];

jsonexport(contacts,function(err, csv){
    if(err) return console.log(err);
    console.log(csv);
});

https://www.npmjs.com/package/jsonexport

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.