I have a dataset in JSON that looks like this:
>>> finalJSON
'"company": {"name": "Micro inc.", "founders": {"name": "Jim D", "name": "Susan A"},
"company": {"name": "Vitacore", "founders": {"name": "Billy B", "name": "Sally Q", "name": "Mark G"}'
.....
I need to loop through and send each of these items:
'"company": {"name": "Micro inc.", "founders": {"name": "Jim D", "name": "Susan A"},
to a POST Request like this:
d = []
for company in FinalJSON:
p = requests.post((url + '/ratio'), json=company, headers=headers)
if(p.status_code == 200):
print p.text
d.append(p.text)
else:
print(p.status_code)
print "Error"
Edit/Update
Hopefully this is a more complete example of what I'm exactly trying to do. I have a Pandas DataFrame that contains company names and employees like this:
>>> print name_frame
...
name name name name name
Micro inc. NaN Jim D Susan A NaN NaN
Vitacore Billy B NaN Sally Q Mark G NaN
What I need to do is convert this to a JSON format like this:
finalJSON = {
"company":{
"name": "Micro inc.",
"founders": {
"name": "Jim D",
"name": "Susan A",
}
}
"company":{
"name": "Vitacore",
"founders": {
"name": "Billy B",
"name": "Sall Q",
"name":"Mark G",
}
In a previous question I asked I was advised to try this out to get the desired JSON formate:
finalJSON = []
for company, names in df.iterrows():
names = ['"{0}"'.format(name) for name in names.dropna().tolist()]
names_json_str = ('"name": ' if names else '') + ', "name": '.join(names)
finalJSON.append('"company": {"name": "' + company + '", "founders": {' + names_json_str + '}')
finalJSON = ', '.join(finalJSON)
>>> finalJSON
'"company": {"name": "Micro inc.", "founders": {"name": "Jim D", "name": "Susan A"},
"company": {"name": "Vitacore", "founders": {"name": "Billy B", "name": "Sally Q", "name": "Mark G"}'
So now I'm working with integrating the conversion with the post request:
for company, names in name_frame.iterrows():
names = ['"{0}"'.format(name) for name in names.dropna().tolist()]
names_json_str = ('"name": ' if names else '') + ', "name": '.join(names)
payload = '"company": {"name": "' + company + '", "founders": {' + names_json_str + '}'
p = requests.post((url), json=payload, headers=headers)
if(p.status_code == 200):
print p.text
d.append(p.text)
else:
print(p.status_code)
print "Error"
Though the payload is in fact not considered to be JSON serializable
TypeError: set([' + names_json_str + ']) is not JSON serializable