-3

I have stringified JSON data that contains commas in description fields. The AJAX post fails if I have apostrophes OR commas in the data.
How can I strip the following from my var test = JSON.stringify(data)

test would print out as follows: [{"var1":"0","description":"this, has, commas"},{"var1":"1","description":"more, commas"}]

can I strip out the commas in the description so the JSON string lookes like: [{"var1":"0","description":"this has commas"},{"var1":"1","description":"more commas"}]
and so leaving the commas that separate the objects?
or better yet...:
[{"var1":"0","description":"this \\, has \\, commas"},{"var1":"1","description":"more \\, commas"}]


Data needs to be pushed back to my server and loaded back into my DB after changes, commas and apostrophes need to remain in tact.
test.replace(/,/g,"") of course... gets rid of the commas that separate the objects also, and screws me up.
anyone know regex well, that could suggest a way to replace a "," but "not" when between a "},{" ? (double quotes are for emphasis)
Thanks for any help.

Sam
  • 387
  • 4
  • 17
jochanan
  • 15
  • 1
  • 4
  • 5
    Why not remove the commas from the elements, and then stringify the resulting array? – Taplar Sep 06 '18 at 17:55
  • The data I supplied is only a sample, the data I'm using is enormous. Pulling from a database, making modifications and pushing back to the server, not all the data I pull contain commas. about 50K of data at a time. I might add, it needs to be put back into my oracle DB "with" the commas and apostrophes. – jochanan Sep 06 '18 at 17:58
  • Your problem is this: "The AJAX post fails if I have apostrophes OR commas in the data." You need your posts to work with arbitrary data. – antlersoft Sep 06 '18 at 17:58
  • Can you parse the JSON to an object, then look at each of the object's fields to replace "," with "\,", and re-stringify? – Joe Cullinan Sep 06 '18 at 18:09
  • 1
    Why is the server failing when sent valid data? – Kevin B Sep 06 '18 at 18:13
  • Regex solution to my original post would be ideal, the data I'm working with is too huge for traversing over, besides, the commas and apostrophes need to be in tact for the upload, I need to escape them. – jochanan Sep 06 '18 at 18:26
  • @jochanan There is a Regex answer that was posted below 25 minutes ago. Can you take a look and see if that will work for you? – mhodges Sep 06 '18 at 18:36

1 Answers1

4

How about doing the test.replace with a negative lookahead - https://regex101.com/r/WtHcuO/2/:

var data = JSON.stringify([{"var1":"0","description":"this, has, commas"},{"var1":"1","description":"more, commas"}]);

var stripped = data.replace(/,(?!["{}[\]])/g, "");

console.log(stripped);

Or, if you want to preserve the commas, but escape them, you can replace with \\, instead of ""

var data = JSON.stringify([{"var1":"0","description":"this, has, commas"},{"var1":"1","description":"more, commas"}]);

var stripped = data.replace(/,(?!["{}[\]])/g, "\\,");

console.log(stripped);
mhodges
  • 10,938
  • 2
  • 28
  • 46
combatc2
  • 1,215
  • 10
  • 10