Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting JSON to CSV with missing fieldnames

I am having a hard time converting JSON to csv because the names on some of the records don't show up. For example:

[{device: 1,
  name: 'John',
  age: 25,
  city: 'Denver'
 },
 {device: 2,
  name: 'Jake',
  age: 24,
  city: 'New York'
 },
 {device: 3,
  name: 'Phil',
  age: 23}]

It is further made difficult because it's several thousand rows where sometimes the city is known, other times it's not.

I would like to put these together into a csv and just leave Phil's city blank.

like image 813
Jacob Avatar asked Feb 18 '26 00:02

Jacob


1 Answers

You can use this:

import json
import csv
js = """[{"device": 1,
  "name": "John",
  "age": 25,
  "city": "Denver"
 },
 {"device": 2,
  "name": "Jake",
  "age": 24,
  "city": "New York"
 },
 {"device": 3,
  "name": "Phil",
  "age": 23}]
  """
js = json.loads(js)

with open( 'result.csv', 'w' ) as csv_file:
    writer = csv.writer( csv_file )
    columns =  list({column for row in js for column in row.keys()})
    writer.writerow( columns )
    for row in js:
        writer.writerow([None if column not in row else row[column] for column in columns])

This works even with different column names and larger numbers of columns!

like image 154
Omar Avatar answered Feb 20 '26 17:02

Omar