3

Digging through some project code this evening, I ran across a line of jQuery similar to this:

jQuery.parseJSON(jQuery.parseJSON(json_string)));

I was curious as to why the calls were nested. Taking a closer look at the JSON string, I found that it contained slashes (apparently escaping the quotes - this is a PHP project). The JSON string is similar to this:

"[{\"input_index\": 0, \"secondary_index\": 0, \"street_address\": \"14106 Transportation Ave\", \"last_line\": \"Kennedy Building\"}]"

I separated the calls like so:

var res1 = jQuery.parseJSON(json_string);
var res2 = jQuery.parseJSON(res1);

I found that the first call produced another JSON string, with the slashes removed, similar to this:

[{"input_index": 0, "secondary_index": 0, "street_address": "14106 Transportation Ave", "last_line": "Kennedy Building"}]

The second call to jQuery.parseJSON actually produced a javascript object.

Why does this occur? I would expect the first call to fail.


The jQuery documentation here doesn't mention behavior like this. Granted, I'm still fairly new to jQuery, so I may be missing the obvious.

4
  • Entirely possible, but it still doesn't answer why .parseJSON is behaving this way, right? Commented Jun 5, 2013 at 2:07
  • The original string is just like my example, but contains production values which I can't paste here. Commented Jun 5, 2013 at 2:31
  • Are you sure it's the jQuery implementation of parseJSON that is doing this, and not the browsers native implementation? Be aware that "Where the browser provides a native implementation of JSON.parse, jQuery uses it to parse the string." - api.jquery.com/jQuery.parseJSON Commented Jun 5, 2013 at 2:46
  • @Ducain So what is .parseJSON not doing that it should be? Your output from the first call is valid JSON. It is also a valid object (not a string). See this fiddle. In one the keys are strings, the other plain variables. Both work the same way in JavaScript. In fact to be JSON, the strings (and keys) have to be surrounded by double quotes. Not sure what you're expecting. Please clarify... Commented Jun 5, 2013 at 14:19

1 Answer 1

3

At some point in PHP the JSON is being encoded twice. Quotes have to be escaped in order to make the JSON valid. Strings are valid JSON:

"any string.  Quotes (\") can be included"

This can be encoded repeatedly, but all it would do is add quotes to the outside.

It seems like your PHP code is incorrectly calling json_encode twice. You need to call $.parseJSON as many times as json_encode is called.

Sign up to request clarification or add additional context in comments.

7 Comments

My question is about the behavior of jQuery.parseJSON: am I misunderstanding your answer?
@Ducain what do you expect parseJSON to do that it's not doing?
I expected the first call to fail, and I'm unsure in any case why it returns another string instead of an object.
@Ducain it succeeds because the quotes are properly escaped. As I said in my answer, a string is valid JSON. You can use JSON.parse('"string"'), which returns "string". That can't be parsed again, but "{\"obj\": \"value\"}" returns '{"obj": "value"}' (a string) when parsed, which can in turn be parsed and returns the object.
@Ducain in order to see this in reverse, call JSON.stringify on an object twice
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.