I’m trying to convert a payload into json but I get the a JSONDecodeError: Extra data error.
I need them to be 3 separate payloads to be posted to an API.
"data": {
"CustomerID": "15263",
"Timestamp": "2023-07-28T17:08:23Z",
"Status": "networkOk"
}
"data": {
"CustomerID": "15891",
"Timestamp": "2023-07-28T17:08:23Z",
"Status": "networkOk"
}
"data": {
"CustomerID": "16986",
"Timestamp": "2023-07-28T17:08:23Z",
"Status": "networkOk"
}
import json
payload= '{"CustomerID":"15263","Timestamp":"2023-07-28T15:12:48Z","Status":"networkRisk"},"1":{"CustomerID":"15891","Timestamp":"2023-07-28T15:12:48Z","Status":"networkRisk"},"2":{"CustomerID":"16986","Timestamp":"2023-07-28T15:12:48Z","Status":"networkRisk"}'
payload = json.loads(payload)
payload = json.dumps({"data":payload})
2
Answers
You can use this example to "split" the string into 3 dictionaries. But I recommend fixing how the string is made (to make it correct Json):
Prints:
Hi I read your question and I want to help you…
Change the structure and format of the payload_JSON to a list of objects in a string.
When this string is loaded using json.loads(), it is converted into a list of Python dictionaries. Next, in the for loop, each object in the list is extracted and sent as a separate JSON payload to the API server.
this is the output:
Let us know if it was helpful, otherwise please re-propose your problem.
Happy coding!🚀