0

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?

2
  • Does your csv have only 1 row of data? or does it have several? If the latter, how would you return the JSON? Commented May 4, 2021 at 15:17
  • @QuangHoang no it has thousands of rows. The parameter lines = True is used to output each csv row to a json object on a new line. Commented May 4, 2021 at 16:27

1 Answer 1

1

IIUC try creating the nested object row-wise first, then creating the JSON:

import pandas as pd

csv_file = pd.read_csv("data.csv", sep=",",
                       header=0, index_col=False)

# Create Nested dict (Object)
csv_file['Residence'] = csv_file[['City', 'State', 'Zipcode']].apply(
    lambda s: s.to_dict(), axis=1
)
# Write out Name and Residence Only
csv_file[['Name', 'Residence']].to_json("data.json", orient="records",
                                        lines=True, date_format="iso",
                                        double_precision=10, force_ascii=True,
                                        date_unit="ms", default_handler=None)

data.csv

Name,City,State,Zipcode
John Doe,Akron,OH,44140
Jane Smith,Los Angeles,California,90005

data.json

{"Name":"John Doe","Residence":{"City":"Akron","State":"OH","Zipcode":44140}}
{"Name":"Jane Smith","Residence":{"City":"Los Angeles","State":"California","Zipcode":90005}}
Sign up to request clarification or add additional context in comments.

4 Comments

Looks like that could work - where would your first code block (for creating the nested object) occur in my python example?
Right after read_csv. I updated the answer to include the full code.
I know this is out of scope and would be considered "bonus," but how can I ensure zipcode is output to json as a string instead of integer? I'm trying to call the str method in a couple different places in my code, but I can't get it to work.
before nesting code after read csv_file['Zipcode'] = csv_file['Zipcode'].astype(str)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.