Chapter 3. JSON Data Types

If you’ve already learned a programming language or two, you likely have an understanding of data types. If not, that’s OK too. Let’s take a quick look.

Quick Look at Data Types

Imagine what would happen if you hand a little boy that knows nothing about tools a hammer, and you don’t tell him what it’s for. Property and bodily damage would likely occur. If this child is well behaved and coordinated, we could give this child a set of instructions for using the hammer. Instead of running around damaging things, the child would only ever use it for hammering nails and removing them (it’s a well-behaved child, remember). Additionally, when you say to the child, “Will you pass me the hammer, please?”, he doesn’t hand you the screwdriver. Knowing what something is ahead of time and how to use it is as useful in computing as it is in the real world.

In computing, we most often need to know what type of data we are dealing with because we can do different things with different types of data. I can multiply a number by another number, but I can’t multiply a word by another number. If I have a list of words, I can sort them alphabetically. I can’t sort the number 5 alphabetically. So, in programming, when a method (or function) says, “Will you pass me the number, please?”, if we know what a number is, we won’t make the mistake of passing it the word “ketchup.”

In computer science, there is a set of data types referred to as primitive data types. The word “primitive” evokes imagery of Stone Age cavemen sitting around a fire grunting and sharpening sticks. It’s not that these data types are unrefined like the cavemen; rather, it’s that they are some of the first, most basic types of data. Like modern man and cavemen, some of the more modern and progressive data types in existence have their roots in these primitive data types:

  • Numbers (e.g., 5 or 5.09)
    • Integer
    • Floating-point number
    • Fixed-point number
  • Characters and strings (e.g., “a” or “A” or “apple”)
  • Booleans (i.e., true or false)

In different programming languages, the types of data that are “set in stone” are often referred to as the primitive data types, or the built-in types, of that language. This means that the definition of the type and what can be done with it is unchangeable. The programming language isn’t going to allow you to redefine what it means to add two numbers together. These primitive data types vary from language to language, and will often include additions to the preceding list, such as a byte or reference (or pointer, or handler).

Beyond the primitive data types, there are other data types that are used in most programming languages. These are often referred to as composite data types, because they are a fusion, or compound, of the primitive data types. Composite data types, like a sandcastle, have a structure to them. If we took apart the sandcastle, we could see that the structure was built with sand, sticks, and water. If we took apart the data structure of a composite data type, we would find that it was built with our primitive data types.

One example of a composite data type that is commonly used in programming languages is an enumeration data type. Earlier, I mentioned the sorting of a list alphabetically. A list of words could be represented with different data types in different programming languages (e.g., a list or an array). If we took apart this data structure, we would find that it is made up of the primitive data types of characters or strings. The enumeration data type is a data structure that you can enumerate. I can mention each thing in the structure one by one and I can also count how many there are. See Example 3-1.

Example 3-1. “Let me enumerate the fine qualities of your personality” could be represented in programming as an array literal
[
    "witty",
    "charming",
    "brave",
    "bold"
]

You don’t need to understand the array literal data structure in this example to see that we can mention each of these “fine qualities” one by one, and also establish that there are four of them.

Another composite data type is the object data type. In Chapter 2, we explored the object data type, because JavaScript Object Notation is based on the object literal notation of JavaScript. Additionally, I used JSON to describe the shoes I was wearing (see Example 3-2).

Example 3-2. My shoe described in JSON
{
    "brand": "Crocs",
    "color": "pink",
    "size": 9,
    "hasLaces": false
}

This object literal allows us to see that the object data type here is made up of name-value pairs. If we were to deconstruct this data structure, we would see that it is made up of the primitive data types: string, number, and boolean. Our names ("brand", "color", "size", "hasLaces") in our name-value pairs are all string data types. The values "Crocs" and "pink" are both of the string data type. The value "9" is of the number data type. The value "false" is of the boolean data type.

The JSON Data Types

Though programming languages may vary when it comes to composite types, and even a little when it comes to additional primitive types, most share those primitive types I mentioned earlier in the chapter:

  • Numbers (e.g., 5 or 5.09)
    • Integer
    • Floating-point number
    • Fixed-point number
  • Characters and strings (e.g., “a” or “A” or “apple”)
  • Booleans (i.e., true or false)

The object data type is a data structure that is common to some of the more popular programming languages, such as Java and C#, but not all. With JSON being based on object literal notation and the object data type, you’d think this would be problematic for a data interchange format. After all, the goal of a data format is communicating between two different systems and common ground should be expressed in that format. Remember that the data structure that is the composite data type object can be deconstructed into the primitive types. Even to programming languages that do not have the object data type, once the object data structure is deconstructed into those native types, it is quite friendly.

The JSON data types are:

  • Object
  • String
  • Number
  • Boolean
  • Null
  • Array

The JSON Object Data Type

The JSON object data type is simple. JSON, at its root, is an object. It is a list of name-value pairs surrounded in curly braces. When you create a name-value pair within your JSON that is also an object, your JSON will begin to look nested. In Example 3-3, this is illustrated with a person described with nested objects.

Example 3-3. Nested objects
{
    "person": {
        "name": "Lindsay Bassett",
        "heightInInches": 66,
        "head": {
            "hair": {
                "color": "light blond",
                "length": "short",
                "style": "A-line"
            },
            "eyes": "green"
        }
    }
}

The top-level name-value pair here is person, with the value of an object. This object has three name-value pairs: "name", "heightInInches", and "head". The "name" name-value pair has a string value of "Lindsay Bassett". The "heightInInches" name-value pair has a number value. The "head" name-value pair has an object value. The "hair" name-value pair has an object value as well, with three string data typed name-value pairs: "color", "length", and "style". The "head" object also has an "eyes" name-value pair with a value of "green".

The JSON String Data Type

We briefly explored the JSON string data type earlier in this book with the animal/cat example:

{ "animal" : "cat" }

The value "cat" has a string data type. In the real world, unless this data is for a pet shop, the string values in the data won’t be quite as simple. Even a pet shop might have more to say in its data than a single word like “cat.” Perhaps the shop wants to pass along the details of its latest promotion:

Today at Bob’s Best Pets you can get a free 8 oz. sample bag of Bill’s Kibble with your purchase of a puppy. Just say “Bob’s the best!” at checkout.

The JSON string can be comprised of any of the Unicode characters, and all the characters in that promotional text are valid. A string value must always be surrounded in double quotes.

In “A Story: The Double Quotes of JSON” in Chapter 2, I mentioned that single quotes around a string value are not valid (Example 3-4).

Example 3-4. This is not valid JSON
{
    'title': 'This is my title.',
    'body': 'This is the body.'
}

This can be confusing, especially if you’ve seen JavaScript object literals in the past that use single quotes. In JavaScript, we are allowed to use single quotes or double quotes interchangeably. However, it is important to remember that JSON is not a JavaScript object literal; it is only based on JavaScript object literals. In JSON, only double quotes are allowed for surrounding a string value.

Also mentioned in the previous chapter was how JSON is read by a parser. In the eyes of a parser, when a value begins with a double quote ("), it expects a string of text that will be ended by another double quote. This poses a problem if the string of text contains double quotes in it.

For example, suppose we’re running a pet shop promotion where customers need to say “Bob’s the best!” at checkout to receive a free bag of kibble. If we use the code in Example 3-5, we would run into a problem, because we can’t simply surround the promotional data in double quotes.

Example 3-5. This code won’t work
{
    "promo": "Say "Bob's the best!" at checkout for free 8oz bag of kibble."
}

There are quotes inside of the value, and the parser is going to read that first quote character in front of “Bob” in the promotional text as the end of the string. Then, when the parser finds the remainder of the text just hanging out there and not belonging to a name-value pair, it will produce an error. To deal with this, we must escape our quote inside of any string value by preceding it with a backslash character (), as shown in Example 3-6.

Example 3-6. Using a backslash character to escape quotes inside of strings fixes the problem
{
    "promo": "Say "Bob's the best!" at checkout for free 8oz bag of kibble."
}

This backslash character will tell the parser that the quote is not the end of the string. Once the parser actually loads the string into memory, any backslash character that precedes a quote character will be removed and the text will come out on the other side as intended.

Quotes are not the only thing that need escaping when it comes to the JSON string. Because the backslash character is used to escape other characters, we must also escape the backslash. For example, the JSON shown in Example 3-7, which is meant to communicate the location of my Program Files directory, will produce an error. To fix this problem, we must escape the backslash character by adding another backslash character, as shown in Example 3-8.

Example 3-7. The backslash used in this code will throw an error
{
    "location": "C:Program Files"
}
Example 3-8. The backslash character must be escaped with another backslash character
{
    "location": "C:\Program Files"
}

In addition to the double quote and backslash characters, you must escape the following characters:

  • / (forward slash)
  •  (backspace)
  • f (form feed)
  • (tab)
  • (new line)
  • (carriage return)
  • u followed by hexadecimal characters (e.g., the smiley emoticon, u263A)

The JSON shown in Example 3-9 will produce a parser error because the tab and new line characters must be escaped. Example 3-10 shows how to fix the problem.

Example 3-9. The tab and new line characters used in this JSON will cause an error
{
    "story": "	 Once upon a time, in a far away land 
 there lived a princess."
}
Example 3-10. JSON with tab and new line characters escaped
{
    "story": "\t Once upon a time, in a far away land \n there lived a princess."
}

The JSON Number Data Type

Numbers are a common piece of information to pass around in data. Inventory, money, latitude/longitude, and Earth’s mass are all data that can be represented as numbers; see Example 3-11.

Example 3-11. Representing numbers in JSON
{
    "widgetInventory": 289,
    "sadSavingsAccount": 22.59,
    "seattleLatitude": 47.606209,
    "seattleLongitude": -122.332071,
    "earthsMass": 5.97219e+24
}

A number in JSON can be an integer, decimal, negative number, or an exponent.

My widget inventory is 289. Inventory is typically represented by an integer (or whole number). I don’t typically sell a half of something, so my inventory number will never include a decimal point.

My sad savings account contains $22.59. Though some programming languages do have a data type for money, we typically represent money in JSON as a decimal number and omit the $.

If you take a look at the latitude and longitude for the City of Seattle, you will see they are both decimal numbers, and the longitude is a negative decimal number. Negative numbers are represented with the standard minus sign character preceding the number.

Additionally, I’m representing the very large number of Earth’s mass in kg using E Notation. E Notation is particularly great for scientific data and is a supported number.

The JSON Boolean Data Type

In the English language, two of the simplest answers we have for questions are “yes” or “no.” If you ask your friend the question, “Would you like toast with your eggs?”, he will answer “yes” or “no.”

In computer programming, the boolean data type is simple. It is either true or false. If you ask your computer, “Would you like toast with your eggs?”, it will answer “true” or “false.”

In some programming languages, the literal value for true can be 1, and 0 for false. Sometimes the literal value characters use casing—for example, True or TRUE, and false or FALSE. In JSON, the literal value for the boolean data type is always all lowercase: true or false. Any other casing will produce an error. In Example 3-12, booleans are used to communicate data about my preferences for breakfast and lunch.

Example 3-12. Preferences
{
    "toastWithBreakfast": false,
    "breadWithLunch": true
}

The JSON null Data Type

When we have nothing of something, you might think it appropriate to say there is zero of that something. Right now, I own zero watches. The thing is, zero is a number. This implies we were counting in the first place.

What if there was a standard format of describing someone’s wrist in JSON, which included some attributes? See Examples 3-13 and 3-14.

Example 3-13. Next-door neighbor Bob’s might look like this
{
    "freckleCount": 0,
    "hairy": true,
    "watchColor": "blue"
}
Example 3-14. Mine would look like this
{
    "freckleCount": 1,
    "hairy": false,
    "watchColor": null
}

I don’t have a watch color because I’m not wearing a watch. In programming, null is a way of saying zero, zilch, and none without having to use a number. The value for watch color cannot be defined, therefore it is null.

null should not, however, be confused with undefined, which you might run across in JavaScript. undefined is not a JSON data type, but in JavaScript, undefined is what you get when you try to access an object or a variable that does not exist at all. In JavaScript, undefined has a relationship with an object or variable with both its declared name and value not existing, and null has a relationship only with a value of an object or a variable. null is a value that means “no value.” In JSON, null must always be all lowercase characters.

The JSON Array Data Type

Now let’s explore the array data type. If you aren’t familiar with arrays, that’s OK. Let’s take a quick look at what an array is.

Imagine a container that holds a dozen eggs. The container has 12 available compartments for eggs. When I first bought the eggs, there were 12. This would be an array of size 12, containing 12 eggs. See Example 3-15.

Example 3-15. This is an array of strings (for the sake of simplicity, I will use the string “egg” for each of the eggs in the compartments)
{
    "eggCarton": [
        "egg",
        "egg",
        "egg",
        "egg",
        "egg",
        "egg",
        "egg",
        "egg",
        "egg",
        "egg",
        "egg",
        "egg"
    ]
}

Notice that I have a name-value pair. The name is "eggCarton" and the value is an array. The array is always surrounded in square brackets ([]). Inside the array, we have a list, and each list item is separated by a comma. This might look similar to how we format our name-value pairs, but the key difference is that it is a list of only values. These values can be any of the valid JSON data types (string, number, object, boolean, array, and null).

Now suppose I take out two eggs to make myself eggs over easy with toast for breakfast. My egg carton still has 12 compartments, but two eggs are missing. See Example 3-16.

Example 3-16. I’ve removed two eggs from the carton to make breakfast
{
    "eggCarton": [
        "egg",
        null,
        "egg",
        "egg",
        "egg",
        "egg",
        "egg",
        "egg",
        "egg",
        "egg",
        null,
        "egg"
    ]
}

As you can see, I removed the eggs from two specific compartments. Those compartments became empty because the eggs no longer exist. We represent this with null.

An array has an index for each of the “compartments.” We begin with 0, so the first compartment has an index of 0, the second has an index of 1, and so on. The last compartment will have an index of 11. So, I removed the eggs at the indices of 1 and 10. This is a valid array.

If I put the number 5 in the empty compartment at index 10, in most programming languages this would be an invalid array. See Example 3-17.

Example 3-17. This would not be valid in most programming languages
{
    "eggCarton": [
        "egg",
        null,
        "egg",
        "egg",
        "egg",
        "egg",
        "egg",
        "egg",
        "egg",
        "egg",
        5,
        "egg"
    ]
}

I said “most programming languages,” but in JSON, the mixing and matching of data types is valid. I will tell you why, and then I will tell you why you shouldn’t do this in your JSON.

In JavaScript, you define a variable. For instance, in Example 3-18, we have a variable named something and we assign it the number 5 for its value.

Example 3-18. Defining a variable in JavaScript
var something = 5;

On the very next line, we could change that variable to have a string value (Example 3-19).

Example 3-19. Changing that variable to have a string value
something = "bob";

And we could even change it to have a value of an object (Example 3-20).

Example 3-20. Changing that variable to have a value of an object
something = { person: "bob" };

The value for my var something (variable) can be a number, string, array, null, or object. In most programming languages, variables aren’t allowed to be so shifty. Normally you would declare something, as either an int, a string, or an object. So, when you declared your variable called something, you would say int something = 5. You would say string something = "bob" or you would say Person something = new Person("bob"). So in most programming languages, when you declare an array, you are declaring ahead of time what data type has to be in each of your containers, and you can’t just change it around after the fact.

JSON is a data interchange format. If you hand your JSON array over to someone that is not going to be using your JSON with JavaScript, your array will cause an error when it is being parsed.

For example, suppose you attend a convention where merchants are selling collections of rocks. You have a collection of rocks to sell. A guy shows up and wants to buy your collection of 50 rocks, and he takes them, but when he gets home he finds the rock collection does not contain 50 rocks, but it contains 20 rocks, 20 sticks, and 10 pieces of gum (one of which has already been chewed).

Let’s take a closer look at some examples of arrays of each data type. In JSON, the array can be any of the supported data types. So, we can have an array of strings, an array of numbers, an array of booleans, an array of objects, or an array of arrays. An array of arrays is called a multidimensional array. Let’s take a look at some examples.

Suppose we have a roster with the names of students who signed up for a course. This can be represented using an array of strings (Example 3-21).

Example 3-21. Using an array of strings to represent a roster of students
{
    "students": [
        "Jane Thomas",
        "Bob Roberts",
        "Robert Bobert",
        "Thomas Janerson"
    ]
}

After the students have taken a test, we can use an array of numbers to represent their scores (Example 3-22).

Example 3-22. Using an array of numbers to represent test scores
{
    "scores": [
        93.5,
        66.7,
        87.6,
        92
    ]
}

If we need to create an answer key for a true/false test, we could use an array of booleans (Example 3-23).

Example 3-23. Using an array of booleans to represent the answers of a true/false test
{
    "answers": [
        true,
        false,
        false,
        true,
        false,
        true,
        true
    ]
}

An array of objects could be used to represent the entire test, including questions and answers (Example 3-24).

Example 3-24. Using an array of objects to represent the questions and answers of a test
{
    "test": [
        {
            "question": "The sky is blue.",
            "answer": true
        },
        {
            "question": "The earth is flat.",
            "answer": false
        },
        {
            "question": "A cat is a dog.",
            "answer": false
        }
    ]
}

To represent the scores from three different tests, an array of arrays, or a multidimensional array, could be used (Example 3-25).

Example 3-25. Using an array of arrays to represent the scores from three different tests
{
    "tests": [
        [
            true,
            false,
            false,
            false
        ],
        [
            true,
            true,
            true,
            true,
            false
        ],
        [
            true,
            false,
            true
        ]
    ]
}

Key Terms and Concepts

This chapter covered the following key terms:

JSON string data type
A string value, like "my string", surrounded in double quotes.
JSON boolean data type
A true or false value.
JSON number data type
A number value, like 42, that can be a positive or negative integer, decimal, or exponent.
JSON null data type
A null value represents an empty value.
JSON array data type
An array is a collection or list of values, and the values can have a data type of string, number, boolean, object, or array; the values in an array are surrounded by square brackets ([]) and delimited by a comma.
JSON object data type
The object data type is a set of name-value pairs delimited by a comma and surrounded by curly brackets ({}).

We also discussed these key concepts:

  • The boolean data type value of true or false in JSON is always all lowercase characters (i.e., true, not True or TRUE).
  • The null data type value in JSON is always all lowercase characters (i.e., null, not NULL or Null).
  • One key difference between an object and an array is that an object is a list or collection of name-value pairs and an array is a list or collection of values.
  • Another key difference between an array and an object is that an array’s values should all have the same data type.  
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.185.87