2

I have a problem with JSON strings flowing back and forth between PHP and javascript Ajax calls. The PHP routines are sending JSON encoded strings without quotes around the floating point numbers, i.e.

{"Route_1":[{"lat":-31.526107395574975,"lng":146.31591796875},{"lat":-31.51674215813217,"lng":145.5303955078125}]}}

But when the JSON string is received by the ajax routine, all the floating point numbers have double quotes around them as shown here:

{"Route_1":[{"lat":"-31.526107395574975","lng":"146.31591796875"},{"lat":"-31.51674215813217","lng":"145.5303955078125"}]}

So after a couple of days trying to get to the bottom of it, I have decided the best course of action is just to parse this to remove the double quotes. I can identify the floating point numbers and using .replace and callback. but the compound replace is not working as I would expect it to - that is it is just returning the same string.

Can anyone shed some light on this please? btw this is not the same issue as asking how to parse a JSON string.

var str = '{"Route_1":[{"lat":"-31.526107395574975","lng":"146.31591796875"},{"lat":"-31.51674215813217","lng":"145.5303955078125"}]}'

var regex = /[-+]?[0-9]*\.?[0-9]+/g;

var t = str.replace(regex, function(x) {
  return x.replace(/"/g, "");
})

console.log(t);
Community
  • 1
  • 1
  • 2
    Why would you use regex when you have a perfectly servicable JSON you can convert to an object and then manipulate? – VLAZ Sep 27 '16 at 07:38
  • Although it actually seems that _isn't_ valid JSON. However, my point stands - you want to do stuff with JSON - if you had one that is valid, you shouldn't need to regex it. – VLAZ Sep 27 '16 at 07:40
  • 2
    Possible duplicate of [How to parse a JSONArray and convert in Javascript](http://stackoverflow.com/questions/39398091/how-to-parse-a-jsonarray-and-convert-in-javascript) – VLAZ Sep 27 '16 at 07:45
  • All my transfers are in JSON. On the server side the code '$jsonArray = json_encode( $json );' transfers the correct file but once on the client side the quotes are introduced - it has just proved too time consuming to try to untangle it. –  Sep 27 '16 at 07:53
  • The `str` parameter you have does not hold correct JSON as it is also quoting each object in the array. However, the _values_ in object being quoted is a trivial thing to change _correctly_ when you properly convert the JSON into a JavaScript object, as the linked question shows. It turns your question from "how do I regex this" to "how do I convert a string into a number" which is far safer, far easier and far better defined. Using a regex is both unnecessary and brittle, which makes it the wrong approach. Regex a golden hammer is not. – VLAZ Sep 27 '16 at 07:59
  • I fully agree with you regarding the soundness of staying within a particular environment, but when that environment is introducing all sorts of artefacts (PHP backslashes ) and double quotes your ability to fathom some reasoning and workarounds is difficult and is possible just as bad as introducing post processing. –  Sep 27 '16 at 08:04

1 Answers1

1

You would need to match " as well in the regex. Check below update.

var str = '{"Route_1":[{"lat":"-31.526107395574975","lng":"146.31591796875"},{"lat":"-31.51674215813217","lng":"145.5303955078125"}]}';

var regex = /\"[-+]?[0-9]*\.?[0-9]+\"/g;

var t = str.replace(regex, function(x) {
  return x.replace(/"/g, "");
})

console.log(t);
Pugazh
  • 9,453
  • 5
  • 33
  • 54
  • 1
    1. I disagree that regex is the correct tool for the job here. 2. That's still not correct JSON. – VLAZ Sep 27 '16 at 07:48
  • I am sorry for the error in the JSON. I will correct it above. The syntax error crept in when I was testing the JSON strings being sent from the client. I was using double quotes to see if that was causing the problem. The correct JSON is now shown. Thanks –  Sep 27 '16 at 08:21