I have a set of ndJOSN dataset like the below:
{'ADDRESS_CITY': 'Whittier', 'ADDRESS_LINE_1': '905 Greenleaf Avenue', 'ADDRESS_STATE': 'CA', 'ADDRESS_ZIP': '90402',},
{'ADDRESS_CITY': 'Cedar Falls', 'ADDRESS_LINE_1': '93323 Maplewood Dr', 'ADDRESS_STATE': 'CA', 'ADDRESS_ZIP': '95014'}
I need to pass values from above into an api request, specifically the body in the format below.
data=[
{
"addressee":"Greenleaf Avenue",
"street":"905 Greenleaf Avenue",
"city":"Whittier",
"state":"CA",
"zipcode":"90402",
},
{
"addressee":"93323",
"street":"Maplewood Dr",
"city":"Cedar Falls",
"state":"CA",
"zipcode":"95014",
}
]
As you can see, the Key's are different so I need to change the Key's to align with the correct data and pass them in with the new key names (ie address_line_1 goes to addressee) - and there are going to be 10k addresses in this request.
I did not note it in my first example, but there is an ID associated with each address - I have to remove to make the request,and then add back in. So I ended up solving with the below - anything more pythonic, these feels not so eloquent to me...?
addresses = ndjson.loads(addresses)
data = json.loads(json.dumps(addresses).replace('"ADDRESS_CITY"','"city"').replace('"ADDRESS_LINE_1"','"street"').replace('"ADDRESS_STATE"','"state"').replace('"ADDRESS_ZIP"','"zipcode"'))
ids = []
for i in data:
i['candidates'] = 1
ids.append(i["ID"])
del i["ID"]
response = requests.request("POST", url, json=data)
resp_data = response.json()
a = 0
for i in resp_data:
i['ID'] = ids[a]
x = i['ID'] = ids[a]
a = a + 1