2

We have JSON sent to us by webservice which has double quotes in value part (Key => Value)in the JSON String. Below is how the JSON looks like

[{
        "dp_id": "SDN071076",
        "list1": "Specially Designated Nationals and Blocked Persons [OFAC]",
        "type": "CYBER2",
        "category": "Entity",
        "name": "ITSEC TEAM AKA AMN PARDAZESH KHARAZMI AKA \"IT SECURITY & PENETRATION TESTING TEAM\" AKA IT SECURITY AND PENETRATION TESTING TEAM AKA \"POOYA DIGITAL SECURITY GROUP\"",
        "street1": "Unit 2, No. 129, Mir Ali Akbari St, Motahari Avenue",
        "street2": "",
        "city": "Tehran",
        "state": "",
        "country": "Iran (Islamic Republic of)",
        "ctrycode": "IR",
        "notes": "Additional Sanctions Information - Subject to Secondary Sanctions . [CYBER2].<BR>",
        "frc": "",
        "startdate": "9/14/2017",
        "enddate": "12/30/2099",
        "frserve": "",
        "optionalid": "a011H00000VI7x8QAD",
        "alerttype": "_Y"
    }]

If you look at the name key in the JSON it has double quotes in the value, so we normally to deserialize to a inner class type Apex throws us below error:

Unexpected character ('I' (code 73)): was expecting comma to separate OBJECT entries at [line:1, column:55]

So we tried to replace all the occurence of \" with single quote but the Apex complier treats \" as double quotes using following code:

jsonString.replace('\\"','\'');

But above didn't not work

Is there's a way to achieve what we are trying to do in Apex?

1

You should be passing the JSON string into one of the JSON Class deserialize methods. As the text you have posted is valid JSON, that should just work without any pre-processing being needed. (You can check that the JSON is valid by pasting into e.g. https://jsonformatter.curiousconcept.com/.)

Generally, needing to pre-process is a sign of e.g. double encoding or some other upstream mistake and it is hard to fix such a problem reliably using replace or regex logic. Best to figure out and fix the upstream problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.