Home > Software engineering >  Converting a String to json
Converting a String to json

Time:03-03

The string below I believe is in json format but when I try to load string into a json variable so I can easily query and extract the data points it errors out.

jdata = '<pre>{\n    "askId": "AAABB110-000011",\n    "dateCreated": "2009-09-01T00:00:00.000Z",\n    "dateUpdated": "2021-06-24T00:00:00.000Z",\n    "owners": [\n        {\n            "ownerType": "Service-Level Owner",\n            "VendorId": "000111222"\n        },\n        {\n            "ownerType": "Technical Owner",\n            "CustomerId": "000333444"\n        },\n        {\n            "ownerType": "Business Owner",\n            "ServiceId": "000005556"\n        }\n    ],\n    "createdBy": "SYSTEM",\n    "lastUpdatedBy": "000667778",\n    "applicationName": "Treasury Bank Data",\n    "description": "Process Data",\n    "aliases": [\n        "Treasury Bank Data",\n        "Bank Data",\n        "Bank Reconciliation",\n        "Bank Rec",\n        "SERV-X"\n    ],\n    "billingBusinessSegmentId": 6,\n    "category": {\n        "categoryId": 1,\n        "categoryName": "Application"\n    },\n    "lifecycleStage": {\n        "lifecycleStageId": 3,\n        "lifecycleStageName": "Production"\n    },\n    "acquiredEntity": {\n        "acquiredEntityId": 0,\n        "acquiredEntityName": "Not Applicable"\n    },\n    "enclaveEnvironment": {\n        "enclaveEnvironmentId": 0,\n        "enclaveEnvironmentName": "General Hosting (Internal, Cloud, or Vendor hosted)"\n    },\n    "softwareType": {\n        "softwareTypeId": 7,\n        "softwareTypeName": "Vendor Product"\n    },\n    "infrastructureRequired": false,\n    "uhgHelpdeskRequired": false,\n    "references": [\n        {\n            "referenceType": "disaster-recovery",\n            "referenceValue": "APPX009919"\n        }\n    ]\n}\n</pre>'

jsonData = json.loads(jdata)

Once I try to load the data into jsonData variable i get error below.

Expecting value: line 1 column 1 (char 0)

Is there something wrong with my string not allowing me to load as a json variable?

CodePudding user response:

If you strip off the html tags, you get json:

import json

jdata = '<pre>{\n    "askId": "AAABB110-000011",\n    "dateCreated": "2009-09-01T00:00:00.000Z",\n    "dateUpdated": "2021-06-24T00:00:00.000Z",\n    "owners": [\n        {\n            "ownerType": "Service-Level Owner",\n            "VendorId": "000111222"\n        },\n        {\n            "ownerType": "Technical Owner",\n            "CustomerId": "000333444"\n        },\n        {\n            "ownerType": "Business Owner",\n            "ServiceId": "000005556"\n        }\n    ],\n    "createdBy": "SYSTEM",\n    "lastUpdatedBy": "000667778",\n    "applicationName": "Treasury Bank Data",\n    "description": "Process Data",\n    "aliases": [\n        "Treasury Bank Data",\n        "Bank Data",\n        "Bank Reconciliation",\n        "Bank Rec",\n        "SERV-X"\n    ],\n    "billingBusinessSegmentId": 6,\n    "category": {\n        "categoryId": 1,\n        "categoryName": "Application"\n    },\n    "lifecycleStage": {\n        "lifecycleStageId": 3,\n        "lifecycleStageName": "Production"\n    },\n    "acquiredEntity": {\n        "acquiredEntityId": 0,\n        "acquiredEntityName": "Not Applicable"\n    },\n    "enclaveEnvironment": {\n        "enclaveEnvironmentId": 0,\n        "enclaveEnvironmentName": "General Hosting (Internal, Cloud, or Vendor hosted)"\n    },\n    "softwareType": {\n        "softwareTypeId": 7,\n        "softwareTypeName": "Vendor Product"\n    },\n    "infrastructureRequired": false,\n    "uhgHelpdeskRequired": false,\n    "references": [\n        {\n            "referenceType": "disaster-recovery",\n            "referenceValue": "APPX009919"\n        }\n    ]\n}\n</pre>'

jsonData = json.loads(jdata.lstrip('<pre>').rstrip('</pre>'))
print(jsonData)
  • Related