# Elegantly combine list of records into JSON for a Python post request?

I'm parsing a Pandas DataFrame table for an API post request (using requests, of course). I'm working on building my script in Jupyter Notebooks.

result = df.to_json(orient="records")


I've seen a lot of JSON manipulations with format(), so my first impulse was to use f-strings:

je_json = json.dumps({
'BatchId': '1',
'userId': 'myID',
'journalEntries': f'{parsed_json }'
})


It would be very readable and elegant, but of course it doesn't work as the result is escaped and will not POST:

'{"BatchId": "1", "userId": "myID", "journalEntries": "[{\\"JENumber\\":\\"PR2022-01-23\\",\\"Date\\":\\"01\\\\/23\\\\/2022\\",\\"JEComment\\": [...]


I solved the problem by reconstructing the list/dicts:

je_json = {
'BatchId': '1',
'userId': 'myID',
'journalEntries': ''
}

je_json['journalEntries'] = []

for record in parsed_json:
je_json['journalEntries'].append(record)



Is there a simpler or more elegant solution?

## Request

data_headers = {
'Authorization': str(parsed_data.get('BearerToken')),
'Content-Type': 'application/json',
}

response = requests.request(method="POST",
url=URL,


## Explanation of my testing methodology

I fail to see a reason for the long way around. If you already have parsed JSON, why not use it directly?

result = df.to_json(orient="records")

je_json = json.dumps({
'BatchId': '1',
'userId': 'myID',
'journalEntries': parsed_json
})


It would be even better to not serialize and deserialize the JSON data twice:

je_json = json.dumps({
'BatchId': '1',
'userId': 'myID',
'journalEntries': df.to_dict(orient="records")
})


On a side-note I find it funny, how pandas' method name to_dict() is absolutely misleading, since it returns a list in the above case.

Also, you don't need to serialize the JSON payload manually, since requests will do it for you:

from requests import post


• #1 & #2 worked perfectly. Exactly what I hoped for. Example #3 didn't work without json.dumps() : '{"ResponseStatus":{"ErrorCode":"SerializationException","Message":"Could not deserialize \'application/json\'; Maybe something to do with my request? I posted this line above. Jan 29 at 17:01
• Ok. Just to be clear. With json.dumps() everything works as expected. Help me out here. I'm a noob with JSON and REST. Can I arrive at the same conclusion if, say, I read the requests docs from start to finish? Jan 29 at 20:02
• @xtian They should both work, i.e. a POST request with data=json.dumps(payload) and 'Content-Type': 'application/json' passed in explicitly as one of the headers is equivalent to a request with just json=payload, where the header is set for you in the latter case (see examples at the end of More complicated POST requests). I would double-check your request that uses json= to make sure you're not overlooking anything. Jan 29 at 21:17
• > "double-check your request that uses json=.." That's it. I need to read a good book on requests, because that was just not obvious--until you pointed it out. Jan 30 at 2:05