How To Convert A .csv File Into A .json File

James Phoenix
James Phoenix

Depending upon your situation, you might need to change file formats to correctly upload your data into another platform. This can easily be achieved with Python with the pandas package.


!pip install pandas
import pandas as pd
import json

. . .

Firstly you can load .csv data in pandas with:

pd.read_csv('some_filename.csv')
df = pd.read_csv('ice-cream-example.csv')
df
Ice Cream TypeGBP Price
0Vanilla3.50
1Chocolate2.50
2Strawberry2.25

Then you can transform it into json:

.to_json()

json_data = df.to_json()

print(json_data)
{"Ice Cream Type":{"0":"Vanilla","1":"Chocolate","2":"Strawberry"},"GBP Price":{"0":3.5,"1":2.5,"2":2.25}}

It’s also possible to change the structure of the json data returned by modifying the orient parameter.

For Series objects the default is ‘index’ and the allowed values are:

Unleash Your Potential with AI-Powered Prompt Engineering!

Dive into our comprehensive Udemy course and learn to craft compelling, AI-optimized prompts. Boost your skills and open up new possibilities with ChatGPT and Prompt Engineering.

Embark on Your AI Journey Now!
  • ‘split’
  • ‘records’
  • ‘index’
  • ‘table’

For DataFrames, the default is ‘columns’ and the allowed values are:

  • ‘split’
  • ‘records’
  • ‘index’
  • ‘columns’
  • ‘values’
  • ‘table’
df.to_json(orient='records')
'[{"Ice Cream Type":"Vanilla","GBP Price":3.5},{"Ice Cream Type":"Chocolate","GBP Price":2.5},{"Ice Cream Type":"Strawberry","GBP Price":2.25}]'
df.to_json(orient='index')
'{"0":{"Ice Cream Type":"Vanilla","GBP Price":3.5},"1":{"Ice Cream Type":"Chocolate","GBP Price":2.5},"2":{"Ice Cream Type":"Strawberry","GBP Price":2.25}}'

By including a file name inside of the .to_json() method, the json will be automatically saved:

df.to_json(path_or_buf='saved_data.json' , orient='records')

Alternatively if we don’t include a file name in the path_or_buf parameter, then the result returned from to_json() is a string, which we can easily turn into a local json file with:

with open('json_example.json', 'w') as f:
    json.dump(json_data, f)

TaggedPython For SEO


More Stories

Cover Image for Why I’m Betting on AI Agents as the Future of Work

Why I’m Betting on AI Agents as the Future of Work

I’ve been spending a lot of time with Devin lately, and I’ve got to tell you – we’re thinking about AI agents all wrong. You and I are standing at the edge of a fundamental shift in how we work with AI. These aren’t just tools anymore; they’re becoming more like background workers in our digital lives. Let me share what I’ve…

James Phoenix
James Phoenix
Cover Image for Supercharging Devin + Supabase: Fixing Docker Performance on EC2 with overlay2

Supercharging Devin + Supabase: Fixing Docker Performance on EC2 with overlay2

The Problem While setting up Devin (a coding assistant) with Supabase CLI on an EC2 instance, I encountered significant performance issues. After investigation, I discovered that Docker was using the VFS storage driver, which is known for being significantly slower than other storage drivers like overlay2. The root cause was interesting: the EC2 instance was already using overlayfs for its root filesystem,…

James Phoenix
James Phoenix