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 Type GBP Price
0 Vanilla 3.50
1 Chocolate 2.50
2 Strawberry 2.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:

  • ‘split’
  • ‘records’
  • ‘index’
  • ‘table’

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

Leanpub Book

Read The Meta-Engineer

A practical book on building autonomous AI systems with Claude Code, context engineering, verification loops, and production harnesses.

Continuously updated
Claude Code + agentic systems
View Book
  • ‘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)
Topics
SEO & Marketing

Newsletter

Become a better AI engineer

Weekly deep dives on production AI systems, context engineering, and the patterns that compound. No fluff, no tutorials. Just what works.

Join 306K+ developers. No spam. Unsubscribe anytime.


More Insights

Cover Image for Build Features in the CLI Before the API

Build Features in the CLI Before the API

Make the next app as much of a CLI app as possible. Feature work happens in the terminal, in-process, against the real development database. The HTTP API is a thin adapter you add afterwards, and a lint rule fails CI if it ever gets a route the CLI does not have.

James Phoenix
James Phoenix
Cover Image for Useful Output Across Three Modes of Work

Useful Output Across Three Modes of Work

I compare typing, interactive AI assistance, and automated loops by useful output per hour of my attention, including review, compute, and failure costs.

James Phoenix
James Phoenix