1

This question is about

why the outputs are different

not how can i achieve the proper output.

I am unable to understand why the output of following two scenarios is not the same, even if I am giving the same argument to the JSON.parse() function.

FIRST scenario

obj = {a:"asdf"};
var newObj = JSON.parse(JSON.stringify(obj));     //newObj = {a:"asdf"}

Debugging Debugging view

SECOND scenario

var newObj = JSON.parse("{"a":"asdf"}");        //this gives an error
5
  • The hint you get in your debugger brought you confusion. Even though there you see "{"a":"asdf"}" this is not a properly formatted string. If you start a string with " then you end it with ". Which means, here the string is only "{" and the rest is broken code the browser doesn't know what to do with. As said in answers you can either use ' to encapsulate the whole string as '{"a":"asdf"}' or you escape the inner " as "{\"a\":\"asdf\"}". Commented Nov 4, 2016 at 10:56
  • I understand what you are trying to say but -- JSON.stringify(obj) - returns "{"a":"asdf"}" only, then how does it work in this case. --JSON.parse(JSON.stringify(obj)) Commented Nov 4, 2016 at 12:03
  • It does not. The browser outputs it in the console as "{"a":"asdf"}" but it is actually returning a string that reads {"a":"asdf"}. The browser console decides to encapsulate this in a " because that is the default console behaviour with strings. So you will read "{"a":"asdf"}" but it is actually a type string with a value of {"a":"asdf"}. Commented Nov 4, 2016 at 12:08
  • But doing this -- JSON.parse({"a":"asdf"}) -- also gives an error. Commented Nov 4, 2016 at 12:19
  • Because the parameter you are passing to JSON.parse is an object, not a string. That is why you should do JSON.parse('{"a":"asdf"}'). Check my answer, I try to explain it there. Commented Nov 4, 2016 at 12:31

3 Answers 3

3

The problem is with quotes.

var newObj = JSON.parse('{"a":"asdf"}');

should work correctly.

In Javascript, we use quotes (single or double) to represent a String. When you want to define a String that contains quotes, then you must use different quotes, or escape the quotes using backslash \ character.

var newObj = JSON.parse("{\"a\":\"asdf\"}");

also works fine.

You might think that

var newObj = JSON.parse("{'a':'asdf'}");

would work, but no. In JSON, strings are defined using Double quotes only.

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

1 Comment

I understand what you are trying to say but -- JSON.stringify(obj) - returns "{"a":"asdf"}" only, then how does it work in this case. --JSON.parse(JSON.stringify(obj))
1

why the outputs are different

Because the inputs are different.

FIRST scenario

obj = {a:"asdf"};
var newObj = JSON.parse(JSON.stringify(obj));

Here the input parameter of JSON.parse is JSON.stringify(obj)and this is a string that reads {"a":"asdf"}.

SECOND scenario

var newObj = JSON.parse("{"a":"asdf"}");

Here the input parameter of JSON.parse is a string that reads { and the rest is broken code.

Confusion arises because the console debugger decides all strings should be shown on the console encapsulated with a ", but this is just a way for the console to tell you that this value is of type String. It does not check whether you have " inside and escape them.

The encapsulating " are not part of the string, only a way of telling it is a string.

If console.logging JSON.stringify(obj) gets you "{"a":"asdf"}" try doing alert instead, or document.write. These will not add extra " and you will see that the value of JSON.stringify(obj) is actually {"a":"asdf"}, not "{"a":"asdf"}".

<html><head></head><body>
<script>
function JSONparse(string) {
    document.write(string);
    alert(string);
    console.log(string);
    return JSON.parse(string);
}
var obj = {a:"asdf"};
result = JSONparse(JSON.stringify(obj));
</script>
</body></html>

Comments

0
var newObj = JSON.parse('{"a":"asdf"}');

Comments

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.