Converting JSON to CSV
The Clearinghouse API returns data in JSON, which saves data in name-value pairs. The output is a list ("array") of cases, where each case is a dictionary -- that's where the name-value pairs come into play. (For more general information about JSON, see here.)
If you want to look at the API output in a spreadsheet, you'll want to convert the JSON data to CSV. There are numerous "JSON to CSV" converters online, but with larger JSON files, these online converters may be slow or unable to handle very large files. This guide demonstrates how to use Python to convert JSON to CSV.
Reading a JSON File
If you already use Python to call the API and are looking to manipulate the data in the same stretch of code, skip this section.
If you have saved your API output as a JSON file, then the first step is to read the file:
When reading data, you may find it helpful to decode the data from UTF-8. You can do this with open("your-file.json", encoding="utf-8")
.
You now have a list of case dictionaries in json_data
.
Writing a CSV File
Next, you'll write a CSV file based on the loaded JSON data. The idea is to make a CSV header row out of the names in the name-value pairs of the JSON data and to create a row for every case.
Note: The following code snippet requires Python 3.7 or higher. (Python dictionaries are ordered as of Python 3.7.)
When writing data, you may find it helpful to encode the data as UTF-8. You can do this with open("new-csv-file.csv", "w", encoding="utf-8")
.
You now have a CSV file you can open as a spreadsheet.
Picking Specific Fields
Because opening a large CSV file in, say, Excel might be a little unwieldy, you can also pick and choose which case fields you want in your CSV file. If you want to go this route, this replaces the code snippet above.
For example, if you want to create a CSV file that contains only the id and summary for every case, here's what that would look like:
Last updated