0

I am trying to convert a nested JSON to a dataframe. The JSON file is deeply nested and I am used the meta parameter to specify the nested structure so that all the JSON attributes can be stored as rows and columns- but I am getting the following error:

TypeError: string indices must be integers

Did i not set the meta parameter properly?

import requests
import pandas as pd
import json
from pandas import json_normalize

requestObject=requests.get("https://dummyjson.com/carts")
#print(requestObject)
requestObject_JSON=requestObject.json()
requestObject_JSON_DF_2=pd.json_normalize(requestObject_JSON,record_path='carts',meta=['id','products'['id','title','price','quantity','total','discountPercentage','discountedPrice'],'total','discountedTotal','userId','totalProducts','totalQuantity'])
print(requestObject_JSON_DF_2)
data =\
{'carts': [{'discountedTotal': 1941,
            'id': 1,
            'products': [{'discountPercentage': 8.71,
                          'discountedPrice': 55,
                          'id': 59,
                          'price': 20,
                          'quantity': 3,
                          'title': 'Spring and summershoes',
                          'total': 60},
                         {'discountPercentage': 3.19,
                          'discountedPrice': 56,
                          'id': 88,
                          'price': 29,
                          'quantity': 2,
                          'title': 'TC Reusable Silicone Magic Washing Gloves',
                          'total': 58},
                         {'discountPercentage': 13.1,
                          'discountedPrice': 70,
                          'id': 18,
                          'price': 40,
                          'quantity': 2,
                          'title': 'Oil Free Moisturizer 100ml',
                          'total': 80},
                         {'discountPercentage': 17.67,
                          'discountedPrice': 766,
                          'id': 95,
                          'price': 930,
                          'quantity': 1,
                          'title': 'Wholesale cargo lashing Belt',
                          'total': 930},
                         {'discountPercentage': 17.2,
                          'discountedPrice': 994,
                          'id': 39,
                          'price': 600,
                          'quantity': 2,
                          'title': 'Women Sweaters Wool',
                          'total': 1200}],
            'total': 2328,
            'totalProducts': 5,
            'totalQuantity': 10,
            'userId': 97}, 
           {'discountedTotal': 1942,
            'id': 2,
            'products': [{'discountPercentage': 8.71,
                          'discountedPrice': 55,
                          'id': 59,
                          'price': 20,
                          'quantity': 3,
                          'title': 'Spring and summershoes',
                          'total': 60},
                         {'discountPercentage': 3.19,
                          'discountedPrice': 56,
                          'id': 88,
                          'price': 29,
                          'quantity': 2,
                          'title': 'TC Reusable Silicone Magic Washing Gloves',
                          'total': 58},
                         {'discountPercentage': 13.1,
                          'discountedPrice': 70,
                          'id': 18,
                          'price': 40,
                          'quantity': 2,
                          'title': 'Oil Free Moisturizer 100ml',
                          'total': 80},
                         {'discountPercentage': 17.67,
                          'discountedPrice': 766,
                          'id': 95,
                          'price': 930,
                          'quantity': 1,
                          'title': 'Wholesale cargo lashing Belt',
                          'total': 930},
                         {'discountPercentage': 17.2,
                          'discountedPrice': 994,
                          'id': 39,
                          'price': 600,
                          'quantity': 2,
                          'title': 'Women Sweaters Wool',
                          'total': 1200}],
            'total': 2328,
            'totalProducts': 5,
            'totalQuantity': 10,
            'userId': 98}
          ]}
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Nidhin_toms
  • 707
  • 4
  • 18
  • 29

1 Answers1

0
# keys from data['carts']
data_carts_keys = list(data['carts'][0].keys())

# remove 'products' because it will be unpacked in record_path
data_carts_keys.remove('products')

# create a list of lists of pairs from the json
meta = [['carts', k] for k in data_carts_keys]

# normalize
df = pd.json_normalize(data, record_path=['carts', 'products'], meta=meta)

df

   discountPercentage  discountedPrice  id  price  quantity                                      title  total carts.discountedTotal carts.id carts.total carts.totalProducts carts.totalQuantity carts.userId
0                8.71               55  59     20         3                     Spring and summershoes     60                  1941        1        2328                   5                  10           97
1                3.19               56  88     29         2  TC Reusable Silicone Magic Washing Gloves     58                  1941        1        2328                   5                  10           97
2               13.10               70  18     40         2                 Oil Free Moisturizer 100ml     80                  1941        1        2328                   5                  10           97
3               17.67              766  95    930         1               Wholesale cargo lashing Belt    930                  1941        1        2328                   5                  10           97
4               17.20              994  39    600         2                        Women Sweaters Wool   1200                  1941        1        2328                   5                  10           97
5                8.71               55  59     20         3                     Spring and summershoes     60                  1942        2        2328                   5                  10           98
6                3.19               56  88     29         2  TC Reusable Silicone Magic Washing Gloves     58                  1942        2        2328                   5                  10           98
7               13.10               70  18     40         2                 Oil Free Moisturizer 100ml     80                  1942        2        2328                   5                  10           98
8               17.67              766  95    930         1               Wholesale cargo lashing Belt    930                  1942        2        2328                   5                  10           98
9               17.20              994  39    600         2                        Women Sweaters Wool   1200                  1942        2        2328                   5                  10           98
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158