Skip to content

How to convert CSV data to JSON data using Python?

We can easily convert a CSV data to JSON data using Python, by importing its predefined CSV and JSON libraries.

JSON (JavaScript Object Notation) is a data exchange format that uses human readable text to store and transmit data objects consisting of key value pairs as shown below. It is widely popular as modern data exchange format in modern application development.

{
    "birthplace": "England",
    "name": "David",
    "surname": "Beckham",
    "sport": "football",
    "age": 55
}

What is the difference between Python dictionary and JSON as they look similar?

Python has its own dictionary class (dict) representing hash table data structure and this too will look like a JSON but with differences. Python dict has wide range of utilities like it holds both key and value pairs as objects. However, it can store references to objects in memory in dict, that could never be achieved using just JSON as it just text data formatted in a certain structure. Let’s explore the difference in the below code.

The code imports the json library to process JSON data and pprint library is just used to print the data in better format than just print command.

BECKHAM variable is loaded with the data, note the last comma after age to prove that it is not JSON formatted data. Then the data is converted and held into bekham_json using “json.dumps” function.  Then converted back to dictionary by loading the JSON data using “json.loads” function.

import json
from pprint import pprint
BECKHAM= {
    "birthplace": "England",
    "name": "David",
    "surname": "Beckham",
    "sport": "football",
    "age": 55, 
}
bekham_json = json.dumps(BECKHAM)
back_to_dict = json.loads(bekham_json)
print(bekham_json)
pprint(back_to_dict)

# Output
{"birthplace": "England", "name": "David", "surname": "Beckham", "sport": "football", "age": 55}
{'age': 55,
 'birthplace': 'England',
 'name': 'David',
 'sport': 'football',
 'surname': 'Beckham'}

After we are clear on the differences between dict and JSON in python it will be easier to understand and convert the csv file to JSON.

Read the csv file and convert it into JSON file.

The below code imports the csv and json libraries from Python to process CSV and JSON files. Using “with” operator opens the “players.csv” file in read mode and holds in f then reads the excel file utilizing dictionary reader function to hold the data in reader, finally converting it into list called players. Then open a new file called “players.json” in write mode and do json.dump of players list.

import csv
import json

with open("players.csv", "r") as f:
    reader = csv.DictReader(f)
    players= list(reader)

with open("players.json", "w") as f:
    json.dump(players, f, indent=2)

#output
players.json file will be created in the root directory

Filter the contents of JSON using Python code

Let’s add some logic in between. Filter the players with name starting from “D” and create a JSON file.

Declare a empty list called players_start_with_d and loop through players list using for-in loop. Filter the player by identifying the string “D” on the first index of the field “name”, add that player element in the new list and finally dump the JSON.

import csv
import json

with open("players.csv", "r") as f:
    reader = csv.DictReader(f)
    players= list(reader)

players_start_with_d = []

for player in players:
    if player['name'][0] =="D":
        players_start_with_d.append(player)


with open("players_d.json", "w") as f:
    json.dump(players_d, f, indent=2)

#output
players_d.json file will be created in the root directory
Leave a Reply

Your email address will not be published. Required fields are marked *