15.2. JSON Objects

15.2.1. Object Diagrams

Recall the JSON for the object depicted earlier in the chapter:

{
  "id": 42,
  "name": "bob"
}

This object can also be described using a UML object diagram:

hide circle
skinparam TitleDontSize 0
skinparam classAttributeIconSize 0
set namespaceSeparator none

object ":" as UserObject0 {
  id = 42
  name = "bob"
}

Object diagrams will be used throughout this chapter so that readers can compare what they see in an object diagram to its corresponding representation in JSON.

Note

In the diagram, the : shown in the name area (i.e., the top part) is typically followed by the object’s class name; however, since we have not yet created a formal class to describe the object yet, we leave the class name portion blank. In later examples, that part may be filled in to provide more information.

15.2.2. Simple JSON Objects

To further understand how JSON can be used to represent the state of an object, consider the User class presented below, described using a UML class diagram:

hide circle
skinparam classAttributeIconSize 0
set namespaceSeparator none

class User {
  -id: int
  -username: String
  <<new>> +User(id: int, username: String)
  +getId(): int
  +getUsername(): String
}

The code for the User class can be written in almost any object-oriented programming language, including Java; however, the specific details of the code for that class do not matter so long as we can imagine what objects of the class might look like based on the class diagram. With that in mind, consider the four User objects presented below, each described using a UML object diagram:

hide circle
skinparam TitleDontSize 0
skinparam classAttributeIconSize 0
set namespaceSeparator none

object ": User" as UserObject0 {
  id = 42
  name = "bob"
}

object ": User" as UserObject1 {
  id = 9001
  name = "sue"
}

object ": User" as UserObject2 {
  id = 1000
  name = "eve"
}

object ": User" as UserObject3 {
  id = 2040
  name = "ted"
}

Each of these four objects can be represented using JSON. For convenience, each example below is included in a separate file in the starter code for this chapter. Here is what the objects look like in JSON:

etc/bob.json
{
  "id": 42,
  "name": "bob"
}
etc/sue.json
{
  "id": 9001,
  "name": "sue"
}
etc/eve.json
{
  "id": 1000,
  "name": "eve"
}
etc/ted.json
{
  "id": 2040,
  "name": "ted"
}

To view a JSON file in your terminal emulator, you are encouraged to combine the cat and jq commands using the shell’s pipe operator (i.e., the |) as shown below so that the output produced by cat is used as the input for jq, resulting in a nicer visual experience that includes indentation and colors directly in the output that you see your terminal emulator. Here is a quick example that displays one of the .json files provided in the starter code:

cat etc/bob.json | jq '.'
{
  "id": 42,
  "name": "bob"
}

The jq command reads JSON text from standard input and displays all or some of it based on a filter supplied as one of its command-line arguments. In the example above, the filter is '.', which tells jq to display all of the JSON that it reads. To learn more about the jq command, refer to its manual page using man jq.

If the outer-most object in the JSON is a regular object and not an array, then you can adjust the filter supplied to jq so that it displays only the part of the object that includes the key / variable name after the dot. For example, compare the filters and outputs in the commands below:

cat etc/bob.json | jq '.'
{
  "id": 42,
  "name": "bob"
}
cat etc/bob.json | jq '.id'
42
cat etc/bob.json | jq '.name'
"bob"

15.2.3. JSON Array Objects

The -a option can be used with the jo command to produce the JSON text for an array. Here are two examples, one that produces the JSON for an array of int values and the other that produces the JSON for an array of String objects:

jo -a 2 4 6 8
[2,4,6,8]
jo -a "a" "b" "c" "d"
["a","b","c","d"]

The outputs above are also saved in some of the JSON files included in the starter code. To view them, let’s use the cat and jq commands again:

cat etc/2468.json | jq '.'
[
  2,
  4,
  6,
  8
]
cat etc/abcd.json | jq '.'
[
  "a",
  "b",
  "c",
  "d"
]

You can adjust the filter supplied to jq to display only one of the array elements. For example, compare the filters and outputs in the commands below:

cat etc/2468.json | jq '.[2]'
6
cat etc/abcd.json | jq '.[3]'
"d"

15.2.4. Nested JSON Objects

Nested objects can also be expressed in JSON. For example, the User objects depicted earlier can be the elements of an array or referred to by a variable in some other object. An example for both of these situations is shown below:

etc/users.json
[
  {
    "id": 42,
    "name": "bob"
  },
  {
    "id": 9001,
    "name": "sue"
  },
  {
    "id": 1000,
    "name": "eve"
  },
  {
    "id": 2040,
    "name": "ted"
  }
]
etc/groups.json
{
  "admin": {
    "id": 9001,
    "name": "sue"
  },
  "otherUsers": [
    {
      "id": 42,
      "name": "bob"
    },
    {
      "id": 1000,
      "name": "eve"
    },
    {
      "id": 2040,
      "name": "ted"
    }
  ]
}

hide circle
skinparam TitleDontSize 0
skinparam classAttributeIconSize 0
set namespaceSeparator none

object ": User" as UserObject0 {
  id = 42
  name = "bob"
}

object ": User" as UserObject1 {
  id = 9001
  name = "sue"
}

object ": User" as UserObject2 {
  id = 1000
  name = "eve"
}

object ": User" as UserObject3 {
  id = 2040
  name = "ted"
}

map ": User[]" as UserArray {
  0 *-> UserObject0
  1 *-> UserObject1
  2 *-> UserObject2
  3 *-> UserObject3
}

Object Diagram for etc/users.json

hide circle
skinparam TitleDontSize 0
skinparam classAttributeIconSize 0
set namespaceSeparator none


object ": User" as UserObject0 {
  id = 42
  name = "bob"
}

object ": User" as UserObject1 {
  id = 9001
  name = "sue"
}

object ": User" as UserObject2 {
  id = 1000
  name = "eve"
}

object ": User" as UserObject3 {
  id = 2040
  name = "ted"
}

map ": User[]" as UserArray {
  0 *-> UserObject0
  1 *-> UserObject2
  2 *-> UserObject3
}

map ":" as OuterObject {
  admin *-> UserObject1
  otherUsers *-> UserArray
}

Object Diagram for etc/groups.json