Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Salesforce
  3. Question
Salesforce

How to deserialize escape quotes in JSON?

Asked by David Edwards Feb 8, 2023 751 views 1 answer
Share

About this question

JSON(I am working with): JSONLinter confirms this is a valid JSON.


{
    "errors": [
        {
            "params": {
                "password": "size must be between 4 and 30",
                "loginId": "must match "^[a-zA-Z0-9_]*$""
            }
        }
    ]
}

Attempt 1(Using JSONParser:

String str = '{ "errors": [ { "params": { "password": "size must be between 4 and 30", "loginId": "must match "^[a-zA-Z0-9_]*$"" } } ] }';
JSONParser parser = JSON.createParser(str);
while(parser.nextToken() != null){
    parser.nextToken();
}

Fails:

▸ ERROR: System.JSONException: Unexpected character ('^' (code 94)): was ▸ expecting comma to separate OBJECT entries at input location [1,99] ▸ ERROR: Class.System.JSONParser.nextToken: line 94, column 1 ▸ AnonymousBlock: line 3, column 1 ▸
AnonymousBlock: line 3, column 1
Attempt 2(JSON2Apex option):
public class JSON2Apex {
    public class Errors {
            public Params params;
    }
    public List errors;
    public class Params {
            public String password;
            public String loginId;
    }
}


String str = '{ "errors": [ { "params": { "password": "size must be between 4 and 30", "loginId": "must match "^[a-zA-Z0-9_]*$"" } } ] }';
JSON2Apex obj = (JSON2Apex)JSON.deserialize(str, JSON2Apex.class);
Fails:
▸ ERROR: System.JSONException: Unexpected character ('^' (code 94)): was ▸ expecting comma to separate OBJECT entries at [line:1, column:99] ▸ ERROR: Class.System.JSON.deserialize: line 15, column 1 ▸ AnonymousBlock: line 2, column 1 ▸ AnonymousBlock: line 2, column 1
Attempt 3(deserializeUntyped):
String str = '{ "errors": [ { "params": { "password": "size must be between 4 and 30", "loginId": "must match "^[a-zA-Z0-9_]*$"" } } ] }';
Map jsonObj = (Map)JSON.deserializeUntyped(str);
Fails:
▸ ERROR: System.JSONException: Unexpected character ('^' (code 94)): was ▸ expecting comma to separate OBJECT entries at [line:1, column:99] ▸ ERROR: Class.System.JSON.deserializeUntyped: line 11, column 1 ▸ AnonymousBlock: line 2, column 1 ▸ AnonymousBlock: line 2, column 1
Attempt 4(replace the problem causing double quote in JSON):
String str = '{ "errors": [ { "params": { "password": "size must be between 4 and 30", "loginId": "must match "^[a-zA-Z0-9_]*$"" } } ] }';
Map jsonObj = (Map)JSON.deserializeUntyped(str.replaceAll('"', '\"'));
Still fails:
▸ ERROR: System.JSONException: Unexpected character ('^' (code 94)): was ▸ expecting comma to separate OBJECT entries at [line:1, column:99] ▸ ERROR: Class.System.JSON.deserializeUntyped: line 11, column 1 ▸ AnonymousBlock: line 2, column 1 ▸ AnonymousBlock: line 2, column 1

Your answer

1 Answer

More Salesforce discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Salesforce Blogs

Guides, tips and career advice on Salesforce from JanBask experts.