Sometimes the data given to us includes quotes within quotes which breaks the JSON parser.
Then it is NOT proper JSON, period. Those quotes within quotes must be escaped in the JSON.
It seems it is not possible for them to escape these characters ...
More likely, someone doesn't want to invest the effort to fix the broken software that is generating invalid JSON.
.... so can I escape them on our end?
You won't be escaping at your end. You will be writing a custom JSON parser that copes with certain kinds of brokenness in it input data, and hopefully corrects it to match what the sender intends. It will be effectively guessing where the escapes should have been in the test being parsed.
It will be a lot of work. Have you ever written a parser before?
One of the difficulties will be knowing where the quoted string ought to end.
Compare these:
{"id":"101","user":"username","comment":"Example: "Comment: 123" test" }
{"id":"101","user":"username","comment":"Example: "Comment: 123" test" }" }
In the first case you might expect the "comment" attribute's value to end at test
. But what about the second case? Is the }
part of the attribute value? Why wasn't in in the first case?
The problem is that when you have syntactically incorrect JSON, a correcting parser could be corrected it in many different ways to end up with valid JSON. Knowing which correction is the correct correction is ... umm ... a problem.
I think you should follow Jon Skeet's advice.
Push back.
Tell them to fix the broken JSON at their end.
Tell them that the requirements say >>JSON<< and stuff that doesn't parse according to the standard JSON grammar is NOT JSON.
If they insist:
Bill them for the extra work that is beyond the scope of the agreed requirements; i.e. JSON.
Point out that it may be impossible to write a parser for their stuff that will always interpret their "not JSON" text according to the expectations of the authors. (It depends on how complex the JSON schema is and how wide-spread the incorrect quoting problem is.)