skip to Main Content

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


  1. 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):

    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"}'
    
    # "quick-and-dirty" way to make json.loads work
    payload = json.loads('{"0":' + payload + "}")
    
    for v in payload.values():
        print(v)
    

    Prints:

    {'CustomerID': '15263', 'Timestamp': '2023-07-28T15:12:48Z', 'Status': 'networkRisk'}
    {'CustomerID': '15891', 'Timestamp': '2023-07-28T15:12:48Z', 'Status': 'networkRisk'}
    {'CustomerID': '16986', 'Timestamp': '2023-07-28T15:12:48Z', 'Status': 'networkRisk'}
    
    Login or Signup to reply.
  2. 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.

    import json
    
    # The JSON payload as an array of objects
    payload_json = '[{"CustomerID":"15263","Timestamp":"2023-07-28T15:12:48Z","Status":"networkRisk"},' 
                   '{"CustomerID":"15891","Timestamp":"2023-07-28T15:12:48Z","Status":"networkRisk"},' 
                   '{"CustomerID":"16986","Timestamp":"2023-07-28T15:12:48Z","Status":"networkRisk"}]'
    
    # Load the JSON data as a list of dictionaries
    payload_list = json.loads(payload_json)
    
    # Iterate through each data object and create separate payloads
    for index, data in enumerate(payload_list):
        payload = json.dumps({"data": data})
        # Here, you can post the payload to the API
        print(f"Payload {index+1}: {payload}")
    

    this is the output:

    Payload 1: {"data": {"CustomerID": "15263", "Timestamp": "2023-07-28T15:12:48Z", "Status": "networkRisk"}}
    Payload 2: {"data": {"CustomerID": "15891", "Timestamp": "2023-07-28T15:12:48Z", "Status": "networkRisk"}}
    Payload 3: {"data": {"CustomerID": "16986", "Timestamp": "2023-07-28T15:12:48Z", "Status": "networkRisk"}}
    

    Let us know if it was helpful, otherwise please re-propose your problem.

    Happy coding!🚀

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search