The problem with your desired output is that it is not valid json document,; it's a stream of json documents!

That's okay, if its what you need, but that means that for each document you want in your output, you'll have to call json.dumps.

Since the newline you want separating your documents is not contained in those documents, you're on the hook for supplying it yourself. So we just need to pull the loop out of the call to json.dump and interpose newlines for each document written.

import csv
import json

csvfile = open('file.csv', 'r')
jsonfile = open('file.json', 'w')

fieldnames = ("FirstName","LastName","IDNumber","Message")
reader = csv.DictReader( csvfile, fieldnames)
for row in reader:
    json.dump(row, jsonfile)
    jsonfile.write('\n')
Answer from SingleNegationElimination on Stack Overflow
🌐
ConvertCSV
convertcsv.com › csv-to-json.htm
CSV To JSON Converter
Choose from the following 5 JSON conversions offered by this tool: CSV to JSON - array of JSON structures matching your CSV, nested JSON via column headers, and JSONLines (MongoDB) mode
🌐
GeeksforGeeks
geeksforgeeks.org › python › convert-csv-to-json-using-python
Convert CSV to JSON using Python - GeeksforGeeks
April 28, 2025 - Converting CSV to JSON using Python involves reading the CSV file, converting each row into a dictionary and then saving the data as a JSON file. For example, a CSV file containing data like names, ages and cities can be easily transformed into ...
🌐
Python Examples
pythonexamples.org › python-csv-to-json
Python – Convert CSV to JSON
Initialize a Python List. Read the lines of CSV file using csv.DictReader() function. Convert each line into a dictionary. Add the dictionary to the Python List created in step 1. Convert the Python List to JSON String using json.dumps(). You may write the JSON String to a JSON file.
🌐
CSVJSON
csvjson.com
CSVJSON - CSVJSON
Online Conversion Tools for Developers. CSV, JSON, SQL and JavaScript. Sponsored by Flatfile.
🌐
ConvertCSV
convertcsv.com › json-to-csv.htm
JSON To CSV Converter
See also CSV to JSON and CSV to GeoJSON Plus Convert JSON to XML, XML to JSON, JSON Lint, JSON Formatter and Analyze JSON Paths at ConvertJSON.com
Top answer
1 of 16
280

With the pandas library, this is as easy as using two commands!

df = pd.read_json()

read_json converts a JSON string to a pandas object (either a series or dataframe). Then:

df.to_csv()

Which can either return a string or write directly to a csv-file. See the docs for to_csv.

Based on the verbosity of previous answers, we should all thank pandas for the shortcut.

For unstructured JSON see this answer.

EDIT: Someone asked for a working minimal example:

import pandas as pd

with open('jsonfile.json', encoding='utf-8') as inputfile:
    df = pd.read_json(inputfile)

df.to_csv('csvfile.csv', encoding='utf-8', index=False)
2 of 16
152

First, your JSON has nested objects, so it normally cannot be directly converted to CSV. You need to change that to something like this:

{
    "pk": 22,
    "model": "auth.permission",
    "codename": "add_logentry",
    "content_type": 8,
    "name": "Can add log entry"
},
......]

Here is my code to generate CSV from that:

import csv
import json

x = """[
    {
        "pk": 22,
        "model": "auth.permission",
        "fields": {
            "codename": "add_logentry",
            "name": "Can add log entry",
            "content_type": 8
        }
    },
    {
        "pk": 23,
        "model": "auth.permission",
        "fields": {
            "codename": "change_logentry",
            "name": "Can change log entry",
            "content_type": 8
        }
    },
    {
        "pk": 24,
        "model": "auth.permission",
        "fields": {
            "codename": "delete_logentry",
            "name": "Can delete log entry",
            "content_type": 8
        }
    }
]"""

x = json.loads(x)

f = csv.writer(open("test.csv", "wb+"))

# Write CSV Header, If you dont need that, remove this line
f.writerow(["pk", "model", "codename", "name", "content_type"])

for x in x:
    f.writerow([x["pk"],
                x["model"],
                x["fields"]["codename"],
                x["fields"]["name"],
                x["fields"]["content_type"]])

You will get output as:

pk,model,codename,name,content_type
22,auth.permission,add_logentry,Can add log entry,8
23,auth.permission,change_logentry,Can change log entry,8
24,auth.permission,delete_logentry,Can delete log entry,8
🌐
AskPython
askpython.com › home › convert csv to json using python – a beginner’s guide
Convert CSV to JSON Using Python - A Beginner's Guide - AskPython
January 22, 2022 - import csv import json def csv_to_json(csv_file_path, json_file_path): #create a dictionary data_dict = {} #Step 2 #open a csv file handler with open(csv_file_path, encoding = 'utf-8') as csv_file_handler: csv_reader = csv.DictReader(csv_file_handler) #convert each row into a dictionary #and add the converted data to the data_variable for rows in csv_reader: #assuming a column named 'No' #to be the primary key key = rows['Serial Number'] data_dict[key] = rows #open a json file handler and use json.dumps #method to dump the data #Step 3 with open(json_file_path, 'w', encoding = 'utf-8') as json
Find elsewhere
🌐
Python.org
discuss.python.org › python help
CSV to JSON - Not quite - Python Help - Discussions on Python.org
June 27, 2022 - I am new to this and new to JSON. I thought JSON was always key value pairs and the values were always strings, now I have to be able to export a CSV file to “JSON” using specific data types for each field These are the data types for each key:value pair [{“FIELD_1”: String, “FIELD_2”: Int, “FIELD_1”: Int, “FIELD_1”: Int, “ACTION”: String, “FIELD_1”: String or null, “FIELD_1”: String or null, “FIELD_1”:String, “FIELD_1”: List[String], “FIELD_1”: String, “FIELD_1”: Int or null, “FIEL...
🌐
Teleport
goteleport.com › resources › tools › csv-to-json-converter
CSV to JSON Converter | Instantly Transform CSV to JSON | Teleport
Instantly convert CSV data to JSON format with our free online tool. Simplify your data transformation tasks with fast, accurate results.
🌐
Medium
medium.com › @hannah15198 › convert-csv-to-json-with-python-b8899c722f6d
Convert CSV to JSON with Python. I got help from a youtube tutorial… | by Hannah | Medium
January 7, 2019 - Convert CSV to JSON with Python I got help from a youtube tutorial linked below import csv and import json packages Create a file path to your CSV file: csvFilePath = ‘csv_file_name.csv’ Create a …
🌐
GitHub
github.com › ChrisParsonsDev › csvtojson
GitHub - ChrisParsonsDev/csvtojson: Simple python CSV to JSON converter
Simple python CSV to JSON converter · ##USAGE · Install Python (installed by default on OSX) Download csvtojson.py · From Terminal run $python csvtojson.py input.csv output.json ·
Starred by 4 users
Forked by 11 users
Languages   Python 100.0% | Python 100.0%
🌐
Bonobo-project
bonobo-project.org › how-to › convert-csv-to-json
How to convert CSV to JSON with Bonobo
Convert your CSV files or data to JSON using Bonobo CLI: “bonobo convert input.csv output.json”. Bonobo is a Python 3.5+ framework for ETL jobs.
🌐
Medium
medium.com › @techwithjulles › python-a-program-that-converts-a-csv-file-to-a-json-file-80c18446ac0b
Python — A program that converts a CSV file to a JSON file | by TechwithJulles | Medium
November 16, 2023 - This will read the input.csv file, convert it to a JSON file, and write it to output.json. Let’s say we have a CSV file named input.csv with the following contents: name,age,gender John,30,Male Jane,25,Female Bob,45,Male · After running our Python program, we should have a JSON file named output.json with the following contents:
🌐
Retool
retool.com › utilities › csv-to-json
Convert CSV to JSON | Retool
Upload a CSV file and convert to JSON file using this free tool.
🌐
e-iceblue
e-iceblue.com › Tutorials › Python › Spire.XLS-for-Python › Program-Guide › Conversion › convert-csv-to-json-in-python.html
How to Convert CSV to JSON in Python: Flat, Nested & NDJSON
Using Python and libraries like Spire.XLS for Python, you can: Convert flat CSV files into structured JSON objects. Organize related CSV data into nested JSON structures. Group multiple CSV rows into coherent JSON objects for analysis or APIs. Create JSON Lines (NDJSON) for large datasets or ...
🌐
CSVJSON
csvjson.com › csv2json
CSV to JSON - CSVJSON
Online tool for converting CSV to JSON. Convert Excel to JSON. Transpose data. Output array or hash.
🌐
Steve's Data Tips and Tricks
spsanderson.com › steveondata › posts › 2025-10-01
Working with CSV Files and JSON Data in Python – Steve’s Data Tips and Tricks
October 1, 2025 - import pandas as pd # Read CSV into a DataFrame data = pd.read_csv('data.csv') # Write DataFrame to CSV data.to_csv('output.csv', index=False) Python’s json module makes JSON operations simple:
🌐
Medium
medium.com › analytics-vidhya › cleaning-up-data-and-turning-a-csv-file-into-json-using-python-72e67c2ee76e
Cleaning up data and turning a CSV file into JSON using Python | by Jennifer Takagi | Analytics Vidhya | Medium
March 2, 2021 - The idea was simple: read a CSV file, clean up a bunch information and in the end save all of these on a JSON file. 👩🏻‍💻 · The first step was simple, to import two Pyhton’s modules: json and csv to handle with the files extensions and declare the files input and output that would be read and written on.
🌐
LinkedIn
linkedin.com › pulse › how-convert-csv-json-array-python-rajashekhar-valipishetty-
How to Convert CSV to JSON Array in Python?
April 6, 2022 - Take an empty JSON file and store it in another variable ... Pass the given csv file as an argument to the DictReader() function of the csv module to Convert the given csv file data into a dictionary.