You need to use `newline=""` when using a csv.writer() with Python 3.x, wb is used for Python 2.x versions.

Using the sample JSON you've given, you would just need to iterate over the header fields and create a row from each entry in details. For example:

import json

import csv

data = """{"details":[{"firstName":"nameOfPerson","lastName":"lastNameofPerson","managersEmail":"someEmail", "managersName":"someManager", "departmentName":"someDepartment", "position":"somePosition", "contractStartsDate":"2000-01-01", "contractEndDate":"2000-01-01", "company":"someCompany", "division":"someDivision", "preferredName":"Unknown"}, {"firstName":"nameOfPerson2","lastName":"lastNameofPerson2","managersEmail":"someEmail2","managersName":"someManager2", "departmentName":"someDepartment2", "position":"somePosition2", "contractStartsDate":"2000-02-02", "contractEndDate":"2000-02-02", "company":"someCompany", "division":"someDivision2", "preferredName":"Unknown"} ]}"""

json_data = json.loads(data) header = ["firstName", "lastName", "managersEmail", "contractStartsDate"]

with open("usersData.csv", "w", newline="", encoding="utf-8") as foutput: csvoutput = csv.writer(foutput) csvoutput.writerow(header)

for entry in json_data["details"]:
    csv_output.writerow([entry[key] for key in header])

Giving you:

firstName,lastName,managersEmail,contractStartsDate nameOfPerson,lastNameofPerson,someEmail,2000-01-01 nameOfPerson2,lastNameofPerson2,someEmail2,2000-02-02 If your JSON data contains duplicate entries, then you would have to first load all of the data and remove duplicates before starting to write the rows.

Alternatively, you could use a csv.DictWriter as follows:

import json

import csv

data = """{"details":[{"firstName":"nameOfPerson","lastName":"lastNameofPerson","managersEmail":"someEmail", "managersName":"someManager", "departmentName":"someDepartment", "position":"somePosition", "contractStartsDate":"2000-01-01", "contractEndDate":"2000-01-01", "company":"someCompany", "division":"someDivision", "preferredName":"Unknown"}, {"firstName":"nameOfPerson2","lastName":"lastNameofPerson2","managersEmail":"someEmail2","managersName":"someManager2", "departmentName":"someDepartment2", "position":"somePosition2", "contractStartsDate":"2000-02-02", "contractEndDate":"2000-02-02", "company":"someCompany", "division":"someDivision2", "preferredName":"Unknown"} ]}"""

json_data = json.loads(data) fieldnames = ["firstName", "lastName", "managersEmail", "contractStartsDate"]

with open("usersData.csv", "w", newline="", encoding="utf-8") as foutput: csvoutput = csv.DictWriter(foutput, fieldnames=fieldnames, extrasaction="ignore") csvoutput.writeheader() csvoutput.writerows(jsondata["details"])

To read the data from an input JSON file, you can do the following:

import json

import csv

with open("sourceJSON.json", encoding="utf-8") as finput: jsondata = json.load(f_input)

fieldnames = ["firstName", "lastName", "managersEmail", "contractStartsDate"]

with open("usersData.csv", "w", newline="", encoding="utf-8") as foutput: csvoutput = csv.DictWriter(foutput, fieldnames=fieldnames, extrasaction="ignore") csvoutput.writeheader() csvoutput.writerows(jsondata["details"])

If you need to remove identical rows, then replace the last line with:

csv_output.writerows(dict(t) for t in {tuple(entry.items()) : '' for entry in json_data["details"]})