JSON string to object

Looking at below example


<!DOCTYPE html>
<html>
<body>

<h2>Create Object from JSON String</h2>

<p id="demo"></p>

<script>

var obj = JSON.parse('{ "name":"John", "age":30, "city":"New York"}');
document.getElementById("demo").innerHTML = obj.name + ", " + obj.age;

</script>

</body>
</html>

Open in new window


when they say Object from JSON string which is JSON string which is output object(is it is javascript object or json object or java object or something else)??

please advise
LVL 7
gudii9Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
Just a small hint: JSON.parse()
0
aikimarkCommented:
more hints
* In the following statement
<p id="demo"></p>

Open in new window

"demo" is a string literal

* objects have properties and methods
obj.name refers to the name property of the obj variable
0
Julian HansenCommented:
JSON - JavaScript Object Notation
A way of expressing a JavaScript object as a string so it can be exchanged with other systems in a safe way.

This is a JavaScript object
var myObject {
   name: "Bruce Wayne",
  email: "batman@batcave.com"
};

Open in new window


It can be used like this
console.(myObject.name); // Outputs Bruce Wayne

Open in new window

Now, if we want to send this object say to a server service we can't send the object as this a memory structure that does not translate into sending over a text based medium like an HTTP link. So we need a way to express this object in a way that it can be sent using a text medium. This is where JSON comes in.

If we look at the JSON String for the above we get
'{"name":"Bruce Wayne", "email" : "batman@batcave.com"}'

Open in new window

We can also express it like this (if we want to make it more readable)
{
  "name":"Bruce Wayne", 
  "email" : "batman@batcave.com"
}

Open in new window

Notice that the structure is very similar to our JavaScript object - in fact the only difference is that the keys are enclosed in double quotes.

JavaScript supports keys being enclosed in double quotes so we could take that JSON string and do this
var newObject = {
  "name":"Bruce Wayne", 
  "email" : "batman@batcave.com"
}

Open in new window

And it would be perfectly legal

However, in most instances we are receiving (or sending) JSON from (to) a service - we can't simply assign the string to a variable and expect JavaScript to know it is an object - likewise we can't assign an object to a paramter in a URL an expect JavaScript to express it as a string.

That is where JSON.parse() an JSON.stringify() come in - they are functions that allow us to change a string (JSON) into a JavaScript memory object and vice versa.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

gudii9Author Commented:
var obj = JSON.parse('{ "name":"John", "age":30, "city":"New York"}');

so they are converting json to javascript object
and then they are printign as below??
document.getElementById("demo").innerHTML = obj.name + ", " + obj.age;
That is where JSON.parse() an JSON.stringify() come in - they are functions that allow us to change a string (JSON) into a JavaScript memory object and vice versa.
not clear.
please elaborate
0
Julian HansenCommented:
I can't make it any clearer.
JSON.parse() takes a string - usually from something like an AJAX call and turns it into a memory object that JavaScript understands
JSON.stringify() is the reverse - it takes a JavaScript object and turns it into a string version.

Which part is not clear?
0
gudii9Author Commented:
where can i check json API to see all input type of a method and its return type
please advise
0
gudii9Author Commented:
where can i check json API to see all input type of a method and its return type
please advise
0
Julian HansenCommented:
You can find the documentation for the JSON object here

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.