<< Chapter < Page Chapter >> Page >

The variable named JSONObject

Despite the name, the variable named JSONObject in Listing 13 appears to be an ordinary JavaScript object in object literal format that has nothing to do with JSON.

The keys "name" and "age" in Listing 13 are enclosed in double quotes, which may be a little unusual, but is perfectlyvalid for a JavaScript object. Although JSON keys must be enclosed in quotes, enclosing JavaScript object keys in quotes does not produce a JSON string.

A terminology issue?

This is probably just a terminology issue. However, I don't see anything in the original script at w3schools.com that illustrates anything about JSON.

Stringify the JavaScript object

To illustrate that JSONObject is a JavaScript object (and is not JSON text) , the code in Listing 13 converts it into a JSON string by passing it to the JSON.stringify method.

After discussing the parsing of JSON text, the document at JSON in JavaScript states

"A JSON stringifier goes in the opposite direction, converting JavaScript data structures into JSON text."

The fact that JSON.stringify will accept JSONObject as an incoming parameter and return a JSON string seems to confirm that JSONObject is a JavaScript data structure (an object) .

Parse the JSON string

After displaying the JSON string, Listing 13 calls the JSON.parse method, passing the JSON string as a parameter, to replicate the original JavaScriptobject and displays the values of the object's properties. The screen output is shown in Figure 5 .

Figure 5 . Possible terminology issue.

Missing Figure

Be wary of "JSON objects"

So, the word of caution is, be wary of material that refers to JSON objects. According to Introducing JSON

"JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language."

The reason that JSON is a text format is probably the same reason that XML is a text format. The use of a text format (as opposed to some proprietary object format) ensures that the format can be read by almost any programming language running on almost any computer.

Recognizing the difference

Given all of the above, you might be wondering how to distinguish between a JavaScript object in object literal format and a JSON string. It all comes down to syntax.

Listing 18 presents a simple script that illustrates the difference. Once again, I will discuss it in fragments.

A comparison

Listing 14 shows a JavaScript object in object literal format and a JSON string on two consecutive lines to make them easy to compare.

Listing 14 . Comparison of object and string.
<body><script>var jScrObj01 = {"name":"John","age":33}; var jSonStr01 = '{"name":"Bill","age":33}';document.write("<br/>1. " + jScrObj01.name); document.write("<br/>2. " + jSonStr01.name);document.write("<br/>3. " + jScrObj01); document.write("<br/>4. " + jSonStr01);

The variable named jScrObj01 is (or contains) a JavaScript object.

Questions & Answers

what's Thermochemistry
rhoda Reply
the study of the heat energy which is associated with chemical reactions
Kaddija
How was CH4 and o2 was able to produce (Co2)and (H2o
Edafe Reply
explain please
Victory
First twenty elements with their valences
Martine Reply
what is chemistry
asue Reply
what is atom
asue
what is the best way to define periodic table for jamb
Damilola Reply
what is the change of matter from one state to another
Elijah Reply
what is isolation of organic compounds
IKyernum Reply
what is atomic radius
ThankGod Reply
Read Chapter 6, section 5
Dr
Read Chapter 6, section 5
Kareem
Atomic radius is the radius of the atom and is also called the orbital radius
Kareem
atomic radius is the distance between the nucleus of an atom and its valence shell
Amos
Read Chapter 6, section 5
paulino
Bohr's model of the theory atom
Ayom Reply
is there a question?
Dr
when a gas is compressed why it becomes hot?
ATOMIC
It has no oxygen then
Goldyei
read the chapter on thermochemistry...the sections on "PV" work and the First Law of Thermodynamics should help..
Dr
Which element react with water
Mukthar Reply
Mgo
Ibeh
an increase in the pressure of a gas results in the decrease of its
Valentina Reply
definition of the periodic table
Cosmos Reply
What is the lkenes
Da Reply
what were atoms composed of?
Moses Reply
what is chemistry
Imoh Reply
what is chemistry
Damilola
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Introduction to xml. OpenStax CNX. Dec 02, 2014 Download for free at https://legacy.cnx.org/content/col11207/1.18
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Introduction to xml' conversation and receive update notifications?

Ask