skip to Main Content

As above mentions, I’m currently trying to convert a string into a dictionary that looks like below:

"{"SOAPAction":"http://www.my-website..uk/path/To/service/v1/serviceAndLedger","ID":"1sceas2-61ae-379dd-a9cd-c813fb07u8inb", "info": {"GMT":"16/Nov/2022:12:13:46 +0000", "client":"10.177.147.71:42987", "frontend":"path/to_my_ip/12.189.66.213:8804", "request":"POST /path/service HTTP/1.1", "body":"\/path\/of\/downloaded\/file.xml", "response":"-"}}"

The issue is that the above code currently doesn’t convert into a dictionary due to some invalid JSON formatted content. Because of this, I haven’t been able to use eval, json_loads etc.

Is there any way to reformat the code so that…

A) It is in valid JSON format
B) It is able to convert the above string into a dictionary

2

Answers


  1. Seems you have double-encoded Json(?):

    import json
    
    s = r"{"SOAPAction":"http://www.my-website..uk/path/To/service/v1/serviceAndLedger","ID":"1sceas2-61ae-379dd-a9cd-c813fb07u8inb", "info": {"GMT":"16/Nov/2022:12:13:46 +0000", "client":"10.177.147.71:42987", "frontend":"path/to_my_ip/12.189.66.213:8804", "request":"POST /path/service HTTP/1.1", "body":"\/path\/of\/downloaded\/file.xml", "response":"-"}}"
    
    data = json.loads('"' + s + '"')
    data = json.loads(data)
    
    print(data)
    

    Prints:

    {
        "SOAPAction": "http://www.my-website..uk/path/To/service/v1/serviceAndLedger",
        "ID": "1sceas2-61ae-379dd-a9cd-c813fb07u8inb",
        "info": {
            "GMT": "16/Nov/2022:12:13:46 +0000",
            "client": "10.177.147.71:42987",
            "frontend": "path/to_my_ip/12.189.66.213:8804",
            "request": "POST /path/service HTTP/1.1",
            "body": "/path/of/downloaded/file.xml",
            "response": "-",
        },
    }
    
    Login or Signup to reply.
  2. Another way, with .replace('\"', '"')

    Code:

    import json
    
    input_string = "{"SOAPAction":"http://www.my-website..uk/path/To/service/v1/serviceAndLedger","ID":"1sceas2-61ae-379dd-a9cd-c813fb07u8inb", "info": {"GMT":"16/Nov/2022:12:13:46 +0000", "client":"10.177.147.71:42987", "frontend":"path/to_my_ip/12.189.66.213:8804", "request":"POST /path/service HTTP/1.1", "body":"\/path\/of\/downloaded\/file.xml", "response":"-"}}"
    
    new_string = input_string.replace('\"', '"')
    
    result_dict = json.loads(new_string)
    print(json.dumps(result_dict, indent=4))
        
    

    Output:

    {
        "SOAPAction": "http://www.my-website..uk/path/To/service/v1/serviceAndLedger",
        "ID": "1sceas2-61ae-379dd-a9cd-c813fb07u8inb",
        "info": {
            "GMT": "16/Nov/2022:12:13:46 +0000",
            "client": "10.177.147.71:42987",
            "frontend": "path/to_my_ip/12.189.66.213:8804",
            "request": "POST /path/service HTTP/1.1",
            "body": "/path/of/downloaded/file.xml",
            "response": "-"
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search