<< 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.

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