AnsweredAssumed Answered

Escape unallowed characters in JSON

Question asked by Nicolas Afonso Employee on Nov 23, 2016
Latest reply on Nov 25, 2016 by Stephen_Hughes

Dear community users,


I am currently facing an annoying issue with one of my clients.


I am trying to find a way to cleanly escape unallowed JSON characters in value fields.

Example of malformed JSON coming from backend (double quotes)

"key1": "value 1",
"key2": "valu"e 2"

correction wanted
"key1": "value 1",
"key2": "valu\"e 2"

I managed to deal with the problem with a regular expression but my solution doesn't work if there are mutiple double quotes in the same value.
Here is the expression I used : (: ?"[^"]*)("[^"]*)(",?)
Replacement: $1\\$2$3


Do you have any clean and robust solution to this problem ?

Thank you very much,