Python 3.8.5 with Pandas 1.1.3
I have a csv file with columns: name, city, state, and zipcode. I need to convert to json with the city, state, and zipcode column values inside an object called residence.
For example:
CSV file
Name City State Zipcode
John Doe Akron OH 44140
I need the JSON output to be structured like:
{
"name": "John Doe",
"residence" :
{
"city": "Akron",
"state": "OH",
"zipcode": 44140
}
}
I currently use Pandas to convert csv to json using the following code:
import pandas as pd
csv_file = pd.DataFrame(pd.read_csv("data.csv", sep = ",", header = 0, index_col = False))
csv_file.to_json("data.json", orient = "records", lines = True, date_format = "iso", double_precision = 10, force_ascii = True, date_unit = "ms", default_handler = None)
As-is, that just converts each column to a json key:value.
How can I add to this code to achieve my desired output?
lines = Trueis used to output each csv row to a json object on a new line.