javascript object vs json object

<!DOCTYPE html>
<html>
<body>

<h2>JSONStringFromJavaScriptObjectUsingStringfy_7</h2>

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

<script>

var obj = { "name":"Gary", "age":30, "city":"New York"};
var myJSON = JSON.stringify(obj);
document.getElementById("demo").innerHTML = myJSON;

</script>

</body>
</html>

Open in new window


i am looking at above example

what is java script object there ?

is below is java script object?
var obj = { "name":"Gary", "age":30, "city":"New York"};
or above is json object? or we can call it as both?
please advise
LVL 7
gudii9Asked:
Who is Participating?
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.

zc2Commented:
When the "obj" variable is instantiated it contains a reference on the object described in your code with JS object notation, which includes that curly brackets, colons and other syntax elements.
In other words, a JS object - is a structure in script engine's memory, JSON is a way do describe the content of that object in a text file.
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
gudii9Author Commented:
so below is javascript object you mean?
var obj = { "name":"Gary", "age":30, "city":"New York"};
0
Julian HansenCommented:
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

zc2Commented:
Yes, when this line is executed the js object will be created described by the object notation residing on this line of the source code .
0
Olaf DoschkeSoftware DeveloperCommented:
An object is a blob of binary data in RAM, so you can't write out an object in code.
Therefore we have the "stringified" object notation of javascript objects called JSON.

The HTML/JS code you show makes a roundtrip from JSON notation to create an object variable back to it's JSON notation, which then is shown.

It may become clearer, what that object in RAM means if you work on the object before displaying it back as JSON notation:

<!DOCTYPE html>
<html>
<body>

<h2>JSONStringFromJavaScriptObjectUsingStringfy_7</h2>

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

<script>

var obj = { "name":"Gary", "age":30, "city":"New York"};

/* using two different ways to act on the obj objects properties */
obj.name= "Olaf";
obj.age = 48;
obj["city"]="Hamburg";
    
var myJSON = JSON.stringify(obj);    
document.getElementById("demo").innerHTML = myJSON;

</script>

</body>
</html>

Open in new window


obj is the variable holding the object at runtime, The JSON notation is defining a (vanilla) object with some properties in javascript notation, the notation needed to write this in code.Just like "Gary" is string notation for the string Gary, the double quote string delimiters are just notation, the net string is just the word and name Gary.

JSON notation is having curly brackets as the delimiter and "property":value pairs are the notation of property names and their values, comma separated.

Bye, Olaf.
0
gudii9Author Commented:
The JSON notation is defining a (vanilla) object with some properties in javascript notation, the notation needed to write this in code.Just like "Gary" is string notation for the string Gary, the double quote string delimiters are just notation, the net string is just the word and name Gary.

what is meaning of vanilla object?
please advise
0
zc2Commented:
I think the meaning is explained in this article: https://en.wikipedia.org/wiki/Plain_vanilla
0
Olaf DoschkeSoftware DeveloperCommented:
"vanilla" is not a technical term per se. As zc2 references, along the lines of "the simplest version of something", because we're only dealing with a collection of properties or fields with some value, very simple structured objects.

Bye, Olaf.
0
gudii9Author Commented:
what is inner html here?
please advise
0
Olaf DoschkeSoftware DeveloperCommented:
In this case, again the element with id "demo" is taken. It has no inner HTML at start the tag of the HTML is:

<p id="demo"></p>
This is empty.But at that place the JSON is written. This is intentionally empty at start so what is written and displayed as the paragraph text is the full JSON string.

Bye, Olaf.
0
gudii9Author Commented:
<!DOCTYPE html>
<html>
<body>

<h2>JSONStringFromJavaScriptObjectUsingStringfy_7</h2>

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

<script>

var obj = { "name":"Gary", "age":30, "city":"New York"};

/* using two different ways to act on the obj objects properties */
obj.name= "Olaf";
obj.age = 48;
obj["city"]="Hamburg";
    
var myJSON = JSON.stringify(obj);    
document.getElementById("demo").innerHTML = myJSON;

</script>

</body>
</html>

Open in new window

above gave below output
JSONStringFromJavaScriptObjectUsingStringfy_7

{"name":"Olaf","age":48,"city":"Hamburg"}

so we created object myJSON javascript object and then we are displaying it as JSONString using Stringfy method?
0
gudii9Author Commented:
<!DOCTYPE html>
<html>
<body>

<h2>JSONStringFromJavaScriptObjectUsingStringfy_7 without working on object</h2>

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

<script>

var obj = { "name":"Gary", "age":30, "city":"New York"};


    
var myJSON = JSON.stringify(obj);    
document.getElementById("demo").innerHTML = myJSON;

</script>

</body>
</html>

Open in new window


above gives below output

JSONStringFromJavaScriptObjectUsingStringfy_7 without working on object

{"name":"Gary","age":30,"city":"New York"}
0
gudii9Author Commented:
var myJSON = JSON.stringify(obj);    
document.getElementById("demo").innerHTML = myJSON;
is above is standard syntax?
can i reduce it to one line instead of 2 lines?
0
Julian HansenCommented:
Do you mean
 document.getElementById("demo").innerHTML = JSON.stringify(obj);

Open in new window

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
Web Development

From novice to tech pro — start learning today.