0

How do I escape a double quote (") inside double quotes using a regular expression for the JSON string below?

[{
     "LDAP_ID":"a",
     "LAC_NO":"1153274",
     "ACTION":"VBE",
     "DATE_OF_ACTION":"06-01-2006 AM 12:00:00",
     "RESPONSE":"DPP",
     "DEF_OBSERV":"M",
     "REMARK":"visited b"         s emp & rcd 1 emi",
     "OPR_ID":"FCTV1",
     "ACTION_TO_BE":"",
     "ACTION_TO_BE_DT":"",
     "AMOUNT_TOBECHG":"",
     "DELEGATED_TO":"",
     "BRANCH_CODE":"100",
     "DISP_DATE_OF_ACTION":"06-JAN-06",
     "DISP_ACTION_TO_BE_DT":"",
     "SRNO":"142871",
     "DELETED_FLAG":"",
     "TIMESTAMP":"10-08-2012 AM 11:38:30",
     "STAMPDATETIME":"2012-08-10 11:38:30"
}]

Key line needing escaped:

"REMARK":"visited b"         s emp & rcd 1 emi",
Vicky Gonsalves
  • 11,593
  • 2
  • 37
  • 58

2 Answers2

2

This problem can't be solved with a regular expression. You can even came up with one that works in 99% of the cases, but nothing more.

Invalid JSON is invalid, and must be fixed by a human on the server side. Regex are not intended to solve this kind of problems. You'd better fix it on the server side.

Raffaele
  • 20,627
  • 6
  • 47
  • 86
0

The only way to fix that invalid JSON within JS with regular expressions is to receive it as string, do the replacement and then re-evaluate it as JSON. This last step can be unsafe. Here is a question with a similar problem: Convert object string to JSON

So I would suggest to fix the JSON before receiving it. Anyway, if you can't, here's a solution with regular expressions.

The regular expression only operates between the delimiters of a value :" and ", to make sure to escape only double quotes within a value (edited).

:\s*"[^"]*"[^"]*"\s*(,|\s*\})

Here's the full code with the replacement (regex edited):

var str = '[{ "LDAP_ID":"a", "LAC_NO":"1153274", "ACTION":"VBE", "DATE_OF_ACTION":"06-01-2006 AM 12:00:00", "RESPONSE":"DPP", "DEF_OBSERV":"M", "REMARK":"visited b" s emp & rcd 1 emi", "OPR_ID":"FCTV1", "ACTION_TO_BE":"", "ACTION_TO_BE_DT":"", "AMOUNT_TOBECHG":"", "DELEGATED_TO":"", "BRANCH_CODE":"100", "DISP_DATE_OF_ACTION":"06-JAN-06", "DISP_ACTION_TO_BE_DT":"", "SRNO":"142871", "DELETED_FLAG":"", "TIMESTAMP":"10-08-2012 AM 11:38:30", "STAMPDATETIME":"2012-08-10 11:38:30" }]'

var j = str.replace(/(:\s*"[^"]*)"([^"]*"\s*(,|\s*\}))/g, '$1\\"$2');

var json = JSON.stringify(eval("(" + j + ")"));
Community
  • 1
  • 1
raffazizzi
  • 685
  • 6
  • 13
  • [Seems broken](http://regexpal.com/?flags=g&regex=%3A%5Cs*%22%5B%5E%22%5D%2B%22%5B%5E%22%5D%2B%22%5Cs*%2C&input=%22key%22%20%3A%20%22a%22s%22d%22%2C%20%22key2%22%20%3A%20%22a%22%22%2C%20%22key3%22%20%3A%20%22s%22s%22). If you remove the comma the the end, it's still broken – Raffaele Sep 18 '12 at 08:16
  • Here's a working jsFiddle with the same code as above: http://jsfiddle.net/Y4nYS/ And here's the [regex alone](http://j.mp/OBRtkd) working with the input provided. – raffazizzi Sep 18 '12 at 08:56
  • But you showed a good example. Using zero or more operators instead of one or more fixes it: :\s*"[^"]*"[^"]*"\s*, – raffazizzi Sep 18 '12 at 09:06
  • Still the comma at the prevents it from working if the broken string is the last one in the object :) – Raffaele Sep 18 '12 at 09:12
  • Ok, well spotted again :) What about: :\s*"[^"]*"[^"]*"\s*(,|\s*\}) – raffazizzi Sep 18 '12 at 10:06
  • [No](http://regexpal.com/?flags=g&regex=%3A%5Cs*%22%5B%5E%22%5D*%22%5B%5E%22%5D*%22%5Cs*(%2C%7C%5Cs*%5C%7D)&input=%20%22REMARK%22%3A%20%22foo%3A%20%22bar%22%22%2C) :) still broken. The point is, regular expressions can't work in this context. You could refine and refine it, but the edge case will fatally happen that will break your program. Broken input must be **fixed**, not **worked around** (unless you happen to be a web browser and can't fix a site's markup, but that's another story) – Raffaele Sep 18 '12 at 10:16
  • Yeah, I completely agree with that. The JSON should be well formed to begin with. But it's still and interesting regex problem :) Why is it still broken? It's matching in your latest example. – raffazizzi Sep 18 '12 at 11:38
  • [Simply, I don't think it works](http://jsfiddle.net/KWkuh/) :) Not all quotes are escaped – Raffaele Sep 18 '12 at 12:46
  • The regex is not supposed to escape all the quotes, only those *in between* quotes, as requested in the question. Also the example that you just posted does exactly what's required! – raffazizzi Sep 18 '12 at 13:11