🏛️
CRLCA
  • Overview
  • Quick Start
  • Change Log
  • How-To Guides
    • Querying Multiple Fields
    • Translating a Search URL into an API Query
    • Converting JSON to CSV
    • Embedding PDF Documents
  • API Reference
    • Endpoints
      • Case ID
      • Case Ongoing
      • Case Type
      • Special Collection
      • Case State
      • Case Court
      • Case Status
      • Plaintiffs
        • Public Interest Lawyer
        • Filed Pro Se
        • Class Action Sought
        • Class Action Outcome
        • Attorney Organization
        • Plaintiff Type
      • Defendants
        • Defendant Type
        • Defendant Level
        • Facility type
      • Case Details
        • Cause of Action
        • Available Documents
        • Constitutional Clause
        • Special Case Type
      • Outcome
        • Case Settlement Status
        • Case Relief Source
        • Case Relief Nature
        • Case Content of Injunction
        • Case Prevailing Party
      • Document Details
        • Document Type
      • Issues
        • Case Issue
        • Case Custom Issue
    • Objects
      • Case
        • Case Details
        • Causes of Action
        • Outcome
        • Issues
        • Key Dates
        • Parties
        • Dockets and Case Numbers
        • Documents
        • Resources
        • Summaries
        • Case Status
      • Defendant
      • Docket
      • Docket Entry
      • Document
        • Document Details
        • Citations
      • Resource
        • Resource Details
        • Related Items
  • Need Help?
Powered by GitBook
On this page
  • Reading a JSON File
  • Writing a CSV File
  • Picking Specific Fields
  1. How-To Guides

Converting JSON to CSV

PreviousTranslating a Search URL into an API QueryNextEmbedding PDF Documents

Last updated 1 year ago

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.

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:

import json

# Replace "your-file.json" with your JSON file name
with open("your-file.json") as f:
    json_data = json.load(f)

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.)

import csv

# Replace "new-csv-file.csv" with your desired output file name
with open("new-csv-file.csv", "w") as f:
    writer = csv.writer(f)

    # Write header row of CSV file based on first case in list
    writer.writerow(json_data[0].keys())

    # Iterate through case list to write CSV rows
    for case in json_data:
        writer.writerow(case.values())

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:

import csv

# For more fields that you can add, see
# https://api.clearinghouse.net/api-reference/objects/case
fields = [
    "id",
    "summary",
]

# Replace "new-csv-file.csv" with your desired output file name
with open("new-csv-file.csv", "w") as f:
    writer = csv.writer(f)

    # Write header row
    writer.writerow(fields)

    for case in json_data:
        row = [case[field] for field in fields]
        writer.writerow(row)
here