CodeByte
Published in

CodeByte

JSON to CSV converter in under 10 lines of code

Designed by Author

I’m pretty sure you can do this in fewer lines with a heavy library like pandas. If you just don’t want to do that, here’s how you can do that alternatively.

Assuming the JSON file looks something like this, and we want to have the keys as the column headers in the output CSV file.

Let’s do the imports first:

import json
import csv

We’ll open the input JSON file and load it as a python list data type.

with open('data.json', 'r') as f:
data = json.load(f)

Now we’ll open another file which will be used to save the output CSV content.
We’ll also be traversing over the list we got from the above step and writing its content to a row.

with open('data.csv', 'w') as f:
writer = csv.writer(f)
writer.writerow(data[0].keys()) # column headers
for row in data:
writer.writerow(row.values())

Note that we are using the with____as____ context manager to open the files so that we don’t have to close them manually every time we open something.

This is a very simple and straightforward implementation. This program is assuming that the JSON will have a fixed amount of keys in each row. Of course, we’ll have to write a lot more conditions to check for the keys if it was not the case.

JSON to CSV conversion and vice versa is used widely across many data engineering applications. For example, in the case of ETL pipelines, you might be having multiple sources of data, each having different formats and you are required to transform all that data into a singular format, you can use this type of scripting for that use case.

With this, I’ll finish the article. Hope you found this helpful. Be sure to hop into my discord server here.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Devashish Patil

Devashish Patil

Cloud Engineer at Google | IITR Alumnus