1

Trying to seed a database in django app. I have a csv file that I converted to json and now I need to reformat it to match the django serialization required format found here This is what the json format needs to look like to be acceptable to django (Which looks an awful lot like a dictionary with 3 keys, the third having a value which is a dictionary itself):

[
    {
        "pk": "4b678b301dfd8a4e0dad910de3ae245b",
        "model": "sessions.session",
        "fields": {
            "expire_date": "2013-01-16T08:16:59.844Z",
            ...
        }
    }
]

My json data looks like this after converting it from csv with pandas:

[{'model': 'homepage.territorymanager', 'pk': 1, 'Name': 'Aaron ##', 'Distributor': 'National Energy', 'State': 'BC', 'Brand': 'Trane', 'Cell': '778-###-####', 'email address': None, 'Notes': None, 'Unnamed: 9': None}, {'model': 'homepage.territorymanager', 'pk': 2, 'Name': 'Aaron Martin ', 'Distributor': 'Pierce ###', 'State': 'PA', 'Brand': 'Bryant/Carrier', 'Cell': '267-###-####', 'email address': None, 'Notes': None, 'Unnamed: 9': None},...]

I am using this function to try and reformat

def re_serialize_reg_json(d, jsonFilePath):
    for i in d:
        d2 = {'Name': d[i]['Name'], 'Distributor' : d[i]['Distributor'], 'State' : d[i]['State'], 'Brand' : d[i]['Brand'], 'Cell' : d[i]['Cell'], 'EmailAddress' : d[i]['email address'], 'Notes' : d[i]['Notes']}
        d[i] = {'pk': d[i]['pk'],'model' : d[i]['model'], 'fields' : d2}
    print(d)

and it returns this error which doesn't make any sense because the format that django requires has a dictionary as the value of the third key:

d2 = {'Name': d[i]['Name'], 'Distributor' : d[i]['Distributor'], 'State' : d[i]['State'], 'Brand' : d[i]['Brand'], 'Cell' : d[i]['Cell'], 'EmailAddress' : d[i]['email address'], 'Notes' : d[i]['Notes']}
TypeError: list indices must be integers or slices, not dict

Any help appreciated!

Here is what I did to get d:

df = pandas.read_csv('/Users/justinbenfit/territorymanagerpython/territory managers - Sheet1.csv')
df.to_json('/Users/justinbenfit/territorymanagerpython/territorymanagers.json', orient='records')



jsonFilePath = '/Users/justinbenfit/territorymanagerpython/territorymanagers.json'

def load_file(file_path):
    with open(file_path) as f:
        d = json.load(f)
        return d
d = load_file(jsonFilePath)
print(d)
Justin Benfit
  • 423
  • 3
  • 11
  • 2
    Is `d` a dictionary or a list? Looping over a dictionary gives you the keys of the dictionary, but looping over a list gives you the items in that list. – Nick ODell Oct 21 '21 at 23:23

1 Answers1

0

D is actually a list containing multiple dictionaries, so in order to make it work you want to change that for i in d part to: for i in range(len(d)).

Gvinfinity
  • 124
  • 7
  • thanks! I have updated the original post with what I did to get d in the first place. Is there a way to create it as a dictionary to begin with so I can avoid working with a list? I'm assuming that working with a dictionary to begin with is superior but let me know if it doesn't matter. Thanks! – Justin Benfit Oct 22 '21 at 00:54