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:
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:
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:
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:
{
"id": 42,
"name": "bob"
}
{
"id": 9001,
"name": "sue"
}
{
"id": 1000,
"name": "eve"
}
{
"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:
[
{
"id": 42,
"name": "bob"
},
{
"id": 9001,
"name": "sue"
},
{
"id": 1000,
"name": "eve"
},
{
"id": 2040,
"name": "ted"
}
]
{
"admin": {
"id": 9001,
"name": "sue"
},
"otherUsers": [
{
"id": 42,
"name": "bob"
},
{
"id": 1000,
"name": "eve"
},
{
"id": 2040,
"name": "ted"
}
]
}