In the section called “JSON Syntax” it was mentioned what types are allowed as values in JSON. They are:
The former three are normally, in most programming languages, referred to as primitive types. The latter two are complex, or composite types in that they group several values of various type. String, in the middle, is in some languages a primitive, in others an object.
{
"int_min": -2147483648,
"int_max": 2147483647,
"uint_max": 4294967295,
"long_min": -9223372036854775808,
"long_max": 9223372036854775807,
"ipv6_max": 3.402823669209385e+38,
"ipv6_max_int": 340282366920938463463374607431768211455,
"num253_min": -9007199254740991,
"num253_max": 9007199254740991,
"aarhus_latitude": 56.1629,
"aarhus_longitude": 10.2039,
"nuneaton_lat": 52.5205,
"nuneaton_lon": -1.4654,
"earth_mass": 5.9723e+24,
"earth_radius": 6378000
}
The Valid numbers are seen above. Integers, positive and
negative, decimal numbers, or numbers in so called
scientific notation ie a decimal number multiplied by
a power, e for exponent, of ten.
5.9723e+24 is equivalent to
5.9723 * 1024.
{
"awake": true,
"asleep": false,
"answers": [true, true, true, false, false, true, false]
}
The possible values of booleans are, somewhat surprisingly,
true, and false. They might even
be arranged in an array if that could be useful.
{
"nullIsNotaValue": null
}
Your textbook [Bas15] is a little
ambiguous here.
I prefer the interpretation that formally
null is not a value, it is a keyword for
unknown. It does, however, appear
as a value in a name/value pair ;)
Please notice that true, false,
and null must be all lower case.
{
"firstName": "Niels",
"middleInitial": "M",
"lastName": "Larsen",
"hobby": "Playing the Devil's advocate"
"award": "Won \"King of the Hair Splitters\" in 2015"
}
Please remember that string values as well as names must be quoted with double quotes ("a…b").
{
"name": {
"firstName": "Niels",
"middleInitial": "M",
"lastName": "Larsen"
},
"adresse": {
"street": "Hovedgaden",
"house": 999,
"zip": 8270,
"town": "Højbjerg"
}
}
or even embedded objects
{
"person": {
"name": {
"firstName": "Niels",
"middleInitial": "M",
"lastName": "Larsen"
},
"adresse": {
"street": "Hovedgaden",
"house": 999,
"zip": 8270,
"town": "Højbjerg"
},
"occupation": "Teacher"
}
}
You will, of course, notice the similarity to literal object notation in JavaScript. This is not coincidental. Objects, however, in JSON do not allow functions. That and the fact that names must be quoted are probably the two biggest differences to literal objects
{
"class": [
"Reuben",
"Simeon",
"Judah",
"Issachar",
"Zebulun",
"Benjamin",
"Dan",
"Naphtali",
"Gad",
"Asher",
"Ephraim",
"Manasseh"
]
}
An array named class containing the names of
the twelve students as values. It is feasible that a student
leaves the class, and that perhaps a new student enters the
class at the same time. This would possibly result in:
{
"class": [
"Reuben",
"Simeon",
"Judah",
null,
"Zebulun",
"Benjamin",
"Dan",
"Naphtali",
"Gad",
"Asher",
"Ephraim",
"Manasseh",
{
"name": "The Lost Tribe",
"refound": 2017
}
]
}
Legal JSON? Yes, absolutely: Best pratice? Absolutely not. For quite obvious reasons. How to prevent it? Use JSON schema, and validate your JSON with that.
{
"Image": {
"Width": 800,
"Height": 600,
"Title": "View from 15th Floor",
"Thumbnail": {
"Url": "http://www.example.com/image/481989943",
"Height": 125,
"Width": 100
},
"Animated" : false,
"IDs": [116, 943, 234, 38793]
}
}
[
{
"precision": "zip",
"Latitude": 37.7668,
"Longitude": -122.3959,
"Address": "",
"City": "SAN FRANCISCO",
"State": "CA",
"Zip": "94107",
"Country": "US"
},
{
"precision": "zip",
"Latitude": 37.371991,
"Longitude": -122.026020,
"Address": "",
"City": "SUNNYVALE",
"State": "CA",
"Zip": "94085",
"Country": "US"
}
]