Json tải dấu gạch chéo ngược python

Chuỗi không phải là JSON hợp lệ do các chuỗi thoát

Bạn có ít nhất bốn trường hợp của chuỗi thoát \" (dấu gạch chéo ngược
quote) trong chuỗi của mình, nhưng trong trình thông dịch Python, chúng là thoát
sequences and are interpreted as a plain old quote:

>>> print("abcd\"efgh")
abcd"efgh

Bạn cần thoát khỏi dấu gạch chéo ngược để chúng nằm trong
chuỗi và hiển thị với bộ mã hóa JSON hoặc sử dụng chuỗi thô.

Đã thử nghiệm và hoạt động

Bạn có thể khắc phục sự cố trình tự thoát bằng cách sử dụng chuỗi thô. Ở
bắt đầu lệnh tải JSON của bạn, hãy thay đổi điều này.

x = json.loads('{"message": .. # blah blah blah blah

để này

x = json.loads(r'{"message": .. # blah blah blah blah

Tiền tố r yêu cầu trình thông dịch coi các dấu gạch chéo ngược là các ký tự
bình thường, không phải các chuỗi thoát. Vì vậy, chúng được chèn vào chuỗi
và được chuyển đến bộ mã hóa JSON.

Giải pháp khả thi thứ hai mà tôi chưa thử nghiệm là loại bỏ
dấu gạch chéo ngược bằng cách sử dụng hai dấu gạch chéo ngược liên tiếp. \\, nhưng nếu bạn làm như vậy
bạn có thể thấy rằng câu trích dẫn trần hiện khớp với một câu trích dẫn khác, vì vậy bạn
cần ba(. ) dấu gạch chéo ngược. \\\" thay vào đó. Vì vậy, hãy thử sử dụng dấu gạch chéo ngược nhân đôi-
hoặc dấu gạch chéo ngược nhân ba và xem cách nào hiệu quả.

Hoặc chỉ sử dụng cú pháp chuỗi thô r'...' để thay thế

Đó là một dict được tạo bởi một hệ thống khác và được đặt trong hàng đợi

{
  "eventTimeUtc": "2022-04-18T17:46:31.0372926Z",
  "appId": "xw2IM8jr",
  "sessionId": "cae570cc-1b93-4de6-b7fc-3692a9bb74cf",
  "correlationId": "47b6e817-4f09-4578-b2c5-a126624dae9a",
  "transactionId": "6ccbe933-4c4f-4f56-89ff-f51f070aa434",
  "ipAddress": null,
  "isSystemEvent": false,
  "instanceId": 1,
  "projectId": 8420,
  "userProfileId": 15536,
  "eventType": "fx.Bus.Client.Messages.Background.BackgroundMessage",
  "metadata": {
    "sequenceOrder": "After",
    "dataType": "DocumentActivityHistory",
    "dataTypeMethod": "AddActivityHistory",
    "instance": {
      "id": 1,
      "clientName": "Dev",
      "domain": "http://localhost",
      "instanceUser": {
        "id": 12566,
        "companyId": 13077,
        "companyName": "fx"
      }
    },
    "project": {
      "id": 8420,
      "name": null,
      "groupMembership": {
        "id": 3,
        "name": "Administrators"
      }
    },
    "userProfile": {
      "id": 15536,
      "firstName": "John",
      "lastName": "Smith CA",
      "email": "[email protected]"
    },
    "region": "Canada"
  },
  "webRequest": null,
  "eventData": {
    "BusinessObject": "Background",
    "Data": "[{\"Id\":20737,\"DateCreated\":\"\\/Date(1650303990870-0400)\\/\",\"SessionId\":\"cae570cc-1b93-4de6-b7fc-3692a9bb74cf\",\"ActivityId\":1,\"UserId\":12566,\"UserName\":\"Smith CA, John\",\"UploadedForUser\":null,\"CompanyId\":13077,\"CompanyName\":\"fx\",\"DocumentId\":15340,\"DocumentTitle\":\"doc - Copy (40) - Copy\",\"DocumentType\":null,\"DocumentTypeId\":1,\"DocumentIndex\":\"13\",\"DocumentStatus\":\"Active\",\"IsCleanDocument\":false,\"Version\":null,\"CheckListId\":0,\"CheckListTitle\":\"Testing\",\"CheckListItemId\":6061,\"CheckListItemTitle\":\"Temp\",\"CheckListItemStatus\":\"\",\"CheckListItemSchema\":\"V.C.\",\"Value\":null,\"FullDocumentTitle\":\"doc - Copy (40) - Copy\",\"DocumentDeleted\":false,\"DocumentFilename\":\"doc - Copy (40) - Copy.txt\",\"CheckListSchemaOptionTypeId\":1,\"CheckListActive\":true,\"CheckListItemDeleted\":false,\"FullFolderPath\":\"\\\\Testing\\\\Temp\",\"UserSupport\":false,\"ContentStatus\":null,\"DocumentVersion\":1,\"Xml\":null,\"JobId\":\"6dc01e99-9990-4841-b3aa-f44059956a0f\",\"SecondaryId\":\"c2664d1f-b775-47b6-b44f-76cf61779db8\",\"GroupId\":3,\"GroupName\":\"Administrators\",\"AppId\":\"xw2IM8jr\",\"MessageId\":\"466e34d1-54ee-46a2-a40d-52a46c10e3d1\"},{\"Id\":20738,\"DateCreated\":\"\\/Date(1650303990870-0400)\\/\",\"SessionId\":\"cae570cc-1b93-4de6-b7fc-3692a9bb74cf\",\"ActivityId\":1,\"UserId\":12566,\"UserName\":\"Smith CA, John\",\"UploadedForUser\":null,\"CompanyId\":13077,\"CompanyName\":\"fx\",\"DocumentId\":15341,\"DocumentTitle\":\"doc - Copy (41) - Copy\",\"DocumentType\":null,\"DocumentTypeId\":1,\"DocumentIndex\":\"14\",\"DocumentStatus\":\"Active\",\"IsCleanDocument\":false,\"Version\":null,\"CheckListId\":0,\"CheckListTitle\":\"Testing\",\"CheckListItemId\":6061,\"CheckListItemTitle\":\"Temp\",\"CheckListItemStatus\":\"\",\"CheckListItemSchema\":\"V.C.\",\"Value\":null,\"FullDocumentTitle\":\"doc - Copy (41) - Copy\",\"DocumentDeleted\":false,\"DocumentFilename\":\"doc - Copy (41) - Copy.txt\",\"CheckListSchemaOptionTypeId\":1,\"CheckListActive\":true,\"CheckListItemDeleted\":false,\"FullFolderPath\":\"\\\\Testing\\\\Temp\",\"UserSupport\":false,\"ContentStatus\":null,\"DocumentVersion\":1,\"Xml\":null,\"JobId\":\"6dc01e99-9990-4841-b3aa-f44059956a0f\",\"SecondaryId\":\"1467dbbd-7dcc-4d56-accf-8078ead8f23a\",\"GroupId\":3,\"GroupName\":\"Administrators\",\"AppId\":\"xw2IM8jr\",\"MessageId\":\"bac24110-ea8d-4b9f-8b21-71ff14899d14\"}]",
    "DataType": "DocumentActivityHistory",
    "Action": "",
    "DataTypeMethod": 0,
    "Method": "AddActivityHistory",
    "Result": {
      "Status": null,
      "OutputData": null,
      "Errors": []
    },
    "Name": "DocumentActivityHistory",
    "InstanceId": 1,
    "DealRoomId": 8420,
    "User": {
      "SiteUserId": 12566,
      "SessionId": "cae570cc-1b93-4de6-b7fc-3692a9bb74cf",
      "AppId": "xw2IM8jr",
      "UserProfileId": 15536
    },
    "IncludeDeletedProjects": false,
    "TransactionId": "6ccbe933-4c4f-4f56-89ff-f51f070aa434",
    "ParentTransactionId": null,
    "DateCreated": "2022-04-18T17:46:31.02+00:00",
    "SendReply": true,
    "CorrelationId": "47b6e817-4f09-4578-b2c5-a126624dae9a",
    "Attachment": null,
    "SagaMessagePayloadType": null,
    "SagaMessagePayload": null,
    "SagaReply": null,
    "DataAccess": 2
  }
}

Mảng có tên Data có rất nhiều dấu gạch chéo ngược gây ra sự cố