<< 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 is defense mechanism
Chinaza Reply
what is defense mechanisms
Chinaza
I'm interested in biological psychology and cognitive psychology
Tanya Reply
what does preconceived mean
sammie Reply
physiological Psychology
Nwosu Reply
How can I develope my cognitive domain
Amanyire Reply
why is communication effective
Dakolo Reply
Communication is effective because it allows individuals to share ideas, thoughts, and information with others.
effective communication can lead to improved outcomes in various settings, including personal relationships, business environments, and educational settings. By communicating effectively, individuals can negotiate effectively, solve problems collaboratively, and work towards common goals.
it starts up serve and return practice/assessments.it helps find voice talking therapy also assessments through relaxed conversation.
miss
Every time someone flushes a toilet in the apartment building, the person begins to jumb back automatically after hearing the flush, before the water temperature changes. Identify the types of learning, if it is classical conditioning identify the NS, UCS, CS and CR. If it is operant conditioning, identify the type of consequence positive reinforcement, negative reinforcement or punishment
Wekolamo Reply
please i need answer
Wekolamo
because it helps many people around the world to understand how to interact with other people and understand them well, for example at work (job).
Manix Reply
Agreed 👍 There are many parts of our brains and behaviors, we really need to get to know. Blessings for everyone and happy Sunday!
ARC
A child is a member of community not society elucidate ?
JESSY Reply
Isn't practices worldwide, be it psychology, be it science. isn't much just a false belief of control over something the mind cannot truly comprehend?
Simon Reply
compare and contrast skinner's perspective on personality development on freud
namakula Reply
Skinner skipped the whole unconscious phenomenon and rather emphasized on classical conditioning
war
explain how nature and nurture affect the development and later the productivity of an individual.
Amesalu Reply
nature is an hereditary factor while nurture is an environmental factor which constitute an individual personality. so if an individual's parent has a deviant behavior and was also brought up in an deviant environment, observation of the behavior and the inborn trait we make the individual deviant.
Samuel
I am taking this course because I am hoping that I could somehow learn more about my chosen field of interest and due to the fact that being a PsyD really ignites my passion as an individual the more I hope to learn about developing and literally explore the complexity of my critical thinking skills
Zyryn Reply
good👍
Jonathan
and having a good philosophy of the world is like a sandwich and a peanut butter 👍
Jonathan
generally amnesi how long yrs memory loss
Kelu Reply
interpersonal relationships
Abdulfatai Reply
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