简体   繁体   中英

Convert from CSV to Nested JSON with array in Python

I'm trying to convert from csv to complex nested json. Below is how my CSV looks like

Sample csv

title,section_id,content,expected

"Test case1",1234,"Test step1","Expected Result1"

"Test case1",1234,"Test step2","Expected Result2"

"Test case1",1234,"Test step3","Expected Result3"

Expected output

  'title': 'Test case 1',
  'section_id': 1234,
  'custom_steps_separated': [
    {
      'content': 'Test step1',
      'expected': 'Expected Result 1'
    },
    {
      'content': 'Test step2',
      'expected': 'Expected Result 2'
    },
    {
      'content': 'Test step3',
      'expected': 'Expected Result 3'
    }
  ]
}

Code so far


import pandas as pd
df = pd.read_csv('testcases.csv')
df['custom_steps_separated'] = df[['content','expected']].to_dict('records')
out = df[['title','section_id','custom_steps_separated']].to_json(orient='records', indent=4)
jsondict = json.loads(out)
for i in range(0,len(jsondict)):
 print(list(jsondict)[i])

I hope this will work for your case. You can use groupby

import pandas as pd
df = pd.DataFrame({'title':["Test case1","Test case1","Test case1"],
                               'section_id':['1234','1234','1234'],'content':["Test step1","Test step2","Test step3"],'expected':["Expected Result1","Expected Result2","Expected Result3"]})

df.groupby(['title','section_id']).apply(lambda x: x[['content','expected']].to_dict('records'))
       .reset_index()
       .rename(columns={0:'custom_steps_separated-Data'})
       .to_json(orient='records')

the output what i got is,

[
    {
        "title": "Test case1",
        "section_id": "1234",
        "custom_steps_separated-Data": [
            {
                "content": "Test step1",
                "expected": "Expected Result1"
            },
            {
                "content": "Test step2",
                "expected": "Expected Result2"
            },
            {
                "content": "Test step3",
                "expected": "Expected Result3"
            }
        ]
    }
]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM