Converting JSON to CSV
Last updated
Last updated
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 .)
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.
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:
You now have a list of case dictionaries in json_data
.
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.)
You now have a CSV file you can open as a spreadsheet.
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: