54

A double quote even if escaped is throwing parse error.
look at the code below

//parse the json in javascript  
var testJson = '{"result": ["lunch", "\"Show\""] }';  
var tags = JSON.parse(testJson);  
alert (tags.result[1]);

This is throwing parse error because of the double quotes (which are already escaped).
Even eval() won't work here.
But if i escape it with double slashes like this:

var result = '{"result": ["lunch", "\\"Show\\""] }';  
var tags = JSON.parse(result);  
alert (tags.result[1]);

then it works fine.
Why do we need to use double slash here in javascript? The problem is that PHP json_encode() function escapes a double quote with a single slash (like this: \"show\") which JSON.parse won't be able to parse. How do i handle this situation?

Varun
  • 4,054
  • 6
  • 31
  • 54

9 Answers9

35

Javascript unescapes its strings and json unescapes them as well. the first string ( '{"result": ["lunch", "\"Show\""] }' ) is seen by the json parser as {"result": ["lunch", ""Show""] }, because \" in javascript means ", but doesn't exit the double quoted string.

The second string '{"result": ["lunch", "\\\"Show\\\""] }' gets first unescaped to {"result": ["lunch", "\"Show\""] } (and that is correctly unescaped by json).

I think, that '{"result": ["lunch", "\\"Show\\""] }' should work too.

cube
  • 3,867
  • 7
  • 32
  • 52
34

Well, finally, JSON's parse uses the same eval, so there's no difference when you give them smth. with incorrect syntax. In this case you have to escape correctly your quotes in php, and then escape them and their escaping slashes with json_encode

<?php
    $json = '{"result": ["lunch", "\"Show\""] }';
    echo json_encode($json);
?>

OUTPUT: "{\"result\": [\"lunch\", \"\\\"Show\\\"\"] }"

This should work on client-side JS (if I've made no typos).

Jet
  • 1,171
  • 6
  • 8
  • 2
    But this is something which should be taken care of by the json_encode() function. Why is it returning something which can not be correctly parsed by JSON.parse(). – Varun Jun 04 '09 at 14:04
  • 7
    It's not a bug in json_encode (I'm assuming that's what you meant). json_encode is not intended to create JavaScript string literals, so it doesn't do the additional escaping. – Matthew Crumley Jun 04 '09 at 14:19
11

This problem is caused by the two-folded string escaping mechanism: one comes from JS and one comes from JSON.

A combination of the backslash character combined with another following character is used to represent one character that is not otherwise representable within the string. ''\\'' stands for '\' etc.

This escaping mechanism takes place before JSON.parse() works.

For Example,

var parsedObj = JSON.parse('{"sentence": "It is one backslash(\\\\)"}');
console.log(parsedObj.sentence);
>>>"It is one backslash(\)"

From the string generator's perspective, it passes four backlashes '\' into the JavaScript interpretor.

From the JavaScript interpretor's perspective, it inteprets there are two backlashes(\) as each '\\' sequence will be interpreted as one '\'.

From the JSON parser's perspective, it receives two backlashes(\\) and the JSON string escape rules will parses it as one single '\' which is the output result.

Explain you first code:

var testJson = '{"result": ["lunch", "\"Show\""] }';
//The real string after sequence escaping in to JS is
//'{"result": ["lunch", ""Show""] }' 
//which is passed into the JSON.parse.
//Thus, it breaks the JSON grammar and generates an error
var tags = JSON.parse(testJson);  
alert (tags.result[1]);
steveyang
  • 9,178
  • 8
  • 54
  • 80
10

From the docs

JSON_HEX_APOS (integer) All ' are converted to \u0027
JSON_HEX_QUOT (integer) All " are converted to \u0022

json_encode() takes two args, the value and options. So try

json_encode($result, JSON_HEX_QUOT); // or
json_encode($result, JSON_HEX_QUOT | JSON_HEX_APOS);

I haven't tried this though.

Ólafur Waage
  • 68,817
  • 22
  • 142
  • 198
  • 1
    You are using a version of PHP older than 5.3.0 then, as the docs indicate. – raylu Jul 20 '11 at 23:36
  • That is the answer, but it should have been the default behaviour to begin with –  Jul 18 '12 at 09:43
9

php to javascript Object (php >= 5.3.0)

var storesLocations = JSON.parse('<?php echo addslashes(json_encode($storesLocations,JSON_HEX_APOS | JSON_HEX_QUOT)) ?>');
Community
  • 1
  • 1
valir
  • 371
  • 1
  • 4
  • 10
3

Turn off magic_quotes_gpc in php.ini.

felipe.zkn
  • 2,012
  • 7
  • 31
  • 63
theo
  • 49
  • 1
0

If the standard C escapes are added, then JSON.parse will convert sequences like \" into ", \\ into \, \n into a line-feed, etc.

'foo\\bar\nbaz"' === JSON.parse('"foo\\\\bar\\nbaz\\""')

In our project's case:

original string ""{\"lat\":28.4594965,\"lng\":77.0266383}""

After passing to JSON.parse()

"{"lat":28.4594965,"lng":77.0266383}"

On 2nd pass to JSON.parse()

{lat: 28.4594965, lng: 77.0266383}

Notice that JSON.parse() removed escaping characters instead of converting string to object.

After the escaping characters were removed, the string to object conversion worked.

Here is the demo:

while (typeof p1 != 'object') {
  p1 = JSON.parse(p1);
  pass++;
  console.log('On pass ', pass, p1);
}
Zameer Ansari
  • 28,977
  • 24
  • 140
  • 219
0

This might help:

<?php
$json = '{"result": ["lunch", "\"Show\""] }';
echo addslashes(json_encode($json));
Shahroq
  • 969
  • 1
  • 8
  • 15
0

Try This. and then try commented

<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<pre><?php ($_POST)?print_r($_POST):'' ?></pre>

<form method="POST">
    <input type="text" name="name"><br>
    <input type="email" name="email"><br>
    <input type="time" name="time"><br>
    <input type="date" name="date"><br>
    <input type="hidden" name="id"><br>
    <textarea name="detail"></textarea>
    <input type="submit" value="Submit"><br>
</form>
<?php 
/* data */
$data = [
            'name'=>'vinay"'."'\\\\",
            'email'=>'imvsrajput@demo.demo',
            'time'=>date('H:i:00'),
            'date'=>date('Y-m-d'),
            'detail'=>'Try this var_dump(0=="ZERO") \\ \\"'." ' \\\\    ",
            'id'=>123,
        ];
?>
<span style="display: none;" class="ajax-data"><?=json_encode($data)?></span>
<script type="text/javascript">
    /* Error */
    // var json = JSON.parse('<?=json_encode($data)?>');
    /* Error solved */
    var json = JSON.parse($('.ajax-data').html());
    console.log(json)
    /* automatically assigned value by name attr */
    for(x in json){
        $('[name="'+x+'"]').val(json[x]);
    }
</script>
vinay
  • 1
  • 2