0

I have a json in a string which I want to deserialize.

{
    "Envelope": {
        "Body": {
            "ObtainDecisionDataResponse": {
                "ObtainDecisionDataResult": {
                    "response_message_list": {
                        "message_info": {
                            "message_code": "0x8004278f",
                            "message_detail": "cvc-pattern-valid: Value '14620999' is not facet-valid with respect to pattern '((\d{5}|[A-Z]\d[A-Z]\d[A-Z]\d)?)' for type '#AnonType_business_business_infoAddress'."
                        },
                        "transaction_status_code": "300",
                        "transaction_status_text": "Unsuccessful"
                    }
                }
            }
        }
    }
}

The json value is assigned to String jsonContent.

String jsonContent = XmlToJSON.convertxmlToJson(jsonValue);
Map<String, Object> mapFromXmlContent = (Map<String, Object>) JSON.deserializeUntyped(jsonContent); // deserialization fails here

I am getting a common.apex.runtime.impl.ExecutionException when trying to deserialize the json string. There seems to be some problem with the json. I think it's the presence of single quote character ('14620999') which is causing the deserialization to fail, but I am not sure. I have tried using escapeSingleQuotes method but still it doesn't work like - Map<String, Object> mapFromXmlContent = (Map<String, Object>) JSON.deserializeUntyped(String.escapeSingleQuotes(jsonContent)); How can I successfully deserialize it ? Should I modify the json string first before trying to deserialize it ?

3
  • 1
    Does this answer your question? How do I get started working with JSON in Apex?
    – glls
    Jun 8, 2021 at 22:24
  • @glls Not really. There seems to be some problem with the json. I think it's the presence of single quote character ('14620999') which is causing the deserialization to fail, but I am not sure. I have tried using escapeSingleQuotes method but still it doesn't work like - Map<String, Object> mapFromXmlContent = (Map<String, Object>) JSON.deserializeUntyped(String.escapeSingleQuotes(jsonContent)); Jun 8, 2021 at 22:30
  • in that case, have you tried modifying the json to confirm that it is indeed the issue?
    – glls
    Jun 8, 2021 at 22:36

2 Answers 2

1

Using e.g. https://codebeautify.org/jsonvalidator, the problem seems to be the \d which is not a valid escaped sequence according to e.g. https://www.json.org/. E.g. \n is OK.

So the string you have posted is not valid JSON; if the JSON is generated using a decent library, then that library would have transformed those into \\d i.e. a literal backslash followed by a d.

If someone else is generating the JSON, ask them to fix that. As a last resort, you could do a hacky search and replace before you parse.

1
  • I did manually use a replaceAll method to replace all \d with \\d just in order to test if deserialization succeeds or not. like - Map<String, Object> mapFromXmlContent = (Map<String, Object>) JSON.deserializeUntyped(jsonContent.replaceAll('\\d', '\\\\d')); The only difference I see is that \d changes to \\d but deserialization still fails. Jun 8, 2021 at 23:16
1

The following works:

String value = '{ "Envelope": { "Body": { "ObtainDecisionDataResponse": { "ObtainDecisionDataResult": { "response_message_list": { "message_info": { "message_code": "0x8004278f", "message_detail": "cvc-pattern-valid: Value \'14620999\' is not facet-valid with respect to pattern \'((\\\\d{5}&#124;[A-Z]\\\\d[A-Z]\\\\d[A-Z]\\\\d)?)\' for type \'#AnonType_business_business_infoAddress\'." }, "transaction_status_code": "300", "transaction_status_text": "Unsuccessful" } } } } } }';
System.debug(JSON.deserializeUnTyped(value));

Keep in mind that if you're trying out the JSON in an Apex string, you need to double up on the escapes \ again in order to get what should be the correct JSON, which itself would be \\d to represent a \d. It's likely you got an error because \d isn't a valid Apex string escape, which is why you got the error.

Also note that, for testing purposes, I also had to escape the ' characters in the JSON as well. JSON itself won't require them to be escaped, so this is fine.

The actual JSON should have \\ for a literal \, and Apex needs \\ for a literal \, so you end up needing a quadruple backslash sequence.

2
  • The problem is that the entire json content is coming as a response. It's basically => String jsonContent = response.getBody(). The value get's assigned from the response itself and I am unable to modify this string after the assignment. Doing a jsonString.replaceAll('\\d', '') doesn't modify the string for some reason. Jun 11, 2021 at 12:50
  • @KnightoftheVale \\d translates to \d in the JSON parser, which is invalid. You would need \\\\d to get it to replace properly, which I feel you shouldn't need to, as that literally changes the message. The far end should be patched to emit proper JSON.
    – sfdcfox
    Jun 11, 2021 at 14:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .