0

So I've got a JSON that'll be coming in apex via an API in the format below:

            "articles": [ '+
'                     { '+
'                         "originalURL": "http://search.eci.gov.in/ae_2008e/", '+
'                         "dateCollected": "2008-12-11", '+
'                         "c6URL": "https://secure.c6-intelligence.com/c6images/0013960000/0013955977.pdf", '+
'                         "categories": [ '+
'                             { '+
'                                 "name": "PEP" '+
'                             } '+
'                         ], '+
'                         "snippet": { '+
'                             "title": "kjlkjl", '+
'                             "summary": "The source provides political exposure detai\"ls of t\"he subject. Please refer to the Political Positions and Linked Persons sections of the profile.", '+
'                             "keywordsMatched": [ '+
'                                 "jklj" '+
'                             ] '+
'                         } '+
'                     } '+
'                 ], '

The problem i am facing here is that some summary fields have values in which double quotes(") are used in the content and this causes the parser class/method to fail. Does anyone have an idea as to how to resolve this.PS: if i try and use replace all to replace ':' or '\"' with any other character all double quotes are replaced

2
  • shouldnt a parser be able to handle those escape characters? If its not a valid json I would ask to get a valid json. Commented Nov 19, 2020 at 8:02
  • How are you getting that JSON formatted value. That itself looks wrong. Double quotes inside values looks like a wrong JSON formatted value detai\"ls. Are you getting that from API or from UI? Commented Nov 20, 2020 at 1:09

1 Answer 1

1

Do examine the incoming JSON carefully. A single \ in front of an embedded " is the correct escaping in JSON.

But to represent a literal \ in an Apex String, as in your code sample, you need a second escaping backslash.

So, for example, this parses and the assert passes:

String s = '{"a": "b\\"d"}';
Map<String, Object> m = (Map<String, Object>) JSON.deserializeUntyped(s);
System.assertEquals(3, ((String)m.get('a')).length());

but this will not parse as \" has already mapped by the Apex compiler to " and so the JSON parser at runtime sees "b"d":

String s = '{"a": "b\"d"}';

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.