Home > Mobile >  Issues with trying to parse JSON into a dictionary
Issues with trying to parse JSON into a dictionary

Time:10-30

when I am trying to convert this JSON string into a Python dictionary, I keep getting an invalid escape error. Below is the code, and the traceback. This is for use with an API that sends out much much more information than this, but this is what I have reduced it down to while maintaining the same error. I have used both \\ and \ inside of this string and get the same result.

import json 

work_orders_inprogress = '''
{
    "ItemCollection": [
        {
            "Header": {
                "ID": "work_orderID",
                "CreationTime": "2021-03-17T12:12:47.02-04:00",
                "DeletionTime": "0001-01-01T00:00:00",
                "DeletionTimeSpecified": true,
                "LastModifiedTime": "2021-10-29T12:12:23.73-04:00",
                "LastModifiedBy": "Mit_rando.Province\\First Last"
            },
            "ID": "work_orderID",
            "Type": "WorkOrder",
            "ScheduledTime": "2021-03-19T12:06:00.937-04:00",
            "PromisedTime": "2021-06-01T19:00:00-04:00",
            "InvoiceTime": "0001-01-01T00:00:00",
            "WorkOrderNumber": 35659,
            "InvoiceNumber": 0,
            "PurchaseOrderNumber": "",
            "Contact": {
                "Header": {
                    "ID": "work_orderID",
                    "CreationTime": "2021-03-09T13:00:06.77-05:00",
                    "DeletionTime": "0001-01-01T00:00:00",
                    "DeletionTimeSpecified": true,
                    "LastModifiedTime": "2021-08-30T15:21:28.25-04:00",
                    "LastModifiedBy": "Mit_rando.province\\First Last"
                },
                "ID": "work_orderID",
                "FileAs": "Business - CASH CUSTOMER",
                "Name": {
                    "Title": "Manager",
                    "Prefix": "",
                    "FirstName": "First",
                    "MiddleName": "",
                    "LastName": "Last",
                    "Suffix": ""
                },
                "Address": {
                    "Title": "Business",
                    "ID": "work_orderID",
                    "Street": "some random st",
                    "City": "cityname",
                    "Province": "XX",
                    "PostalCode": "XXXXX",
                    "Country": "USA"
                },
                "Company": "Company Name - CASH CUSTOMER",
                "Phone1Title": "Business",
                "Phone1": "(720) fakenumber",
                "Phone2Title": "Cell",
                "Phone2": "(303) fakenumber",
                "EmailTitle": "Email",
                "Email": "[email protected]",
                "PreferredContactMethod": "Email",
                "MarketingSource": "",
                "Note": "",
                "NoMessaging": false,
                "NoEmail": false,
                "NoPostCard": false
            }
        }
    ]
}
'''

data = json.loads(work_orders_inprogress)

print(type(data))

Error:

Traceback (most recent call last):
  File "<string>", line 70, in <module>
File "/usr/lib/python3.8/json/__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.8/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.8/json/decoder.py", line 353, in raw_decode
    obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Invalid \escape: line 11 column 54 (char 386)

I have even tried running this on online compilers and get the same result. Thanks!

CodePudding user response:

The problem is with the backslashes. Try to take the raw string:

work_orders_inprogress = r'''...
  • Related