An Introduction to JSON for Non-Programmers
Created 2/19/2025, 12:55:05 AM
- JSON* (short for *JavaScript Object Notation*[1]) is a format for storing data. It is used in a lot of different disparate software, and that software is often used by those who may not be technically minded. This article serves as an introduction to JSON for those who may not be familiar with programming or JavaScript.
[ Table of Contents ]
+ 1. Values
+ 1.1. Numbers
+ 1.2. Strings
+ 1.2.1. Escape Sequences
+ 1.3. Booleans
+ 1.4. Null
+ 1.5. Arrays
+ 1.6. Objects
+ 2. Playground
+ Footnotes
+ References
1. Values
A document formatted in JSON contains /values/, which may be of different /types/. For example, we may have the /value/ `5`, which is of the /type/ `number` (since `5` is well, a number). JSON has a list of permitted types that you may use, each with its rules on how they should be written. Some values may have other values within them (as with the /object/ and /array/ types) while others are more simple. A JSON document consists of a single object, that all other data is contained within.
Below is a list of all possible value types:
1.1. Numbers
Numbers are, well, numbers. These may be whole numbers, but may also contain decimals, or use scientific notation. Only arabic numerals (i.e. 0-9, the numbers we're all familiar with) may be used as a number, and numbers may not start with zero.
Examples:
- `1234` - An integer
- `-5` - A negative number
- `3.14159` - A number with decimals
- `2.1e20` - Scientific notation (this represents 2.1 × 10^20)
- `5.2e-10` - Scientific notation with a negative exponent
1.2. Strings
/String/ is Computer Science jargon for text.* Strings in JSON are enclosed by double quotes (`"`), in order to differentiate them from numbers and other text that may be in a file.
Examples:
- `"foo"` - A string containing the text /foo/
- `"Hello, World!"` - Spaces are also allowed in strings
- `"こんにちは世界!"` - Arbitrary unicode† characters are supported too.
1.2.1. Escape Sequences
A keen reader might now be curious about how you would write quotes themselves inside of a string. After all, if we tried to just put a quote there, A JSON parser would just think it's the end of the string! Situations like this are where something called /escape sequences/ come along. They let us represent things that we would not be able to put in a string otherwise. The way this works is that we start an escape sequence with a backslash (`\`), then put some other characters after it, depending on what we want. For example, if we wanted to put a quote in a string, we would use the sequence `\"`. Of course, now one may ask how you represent `\` itself? Well, just put another backslash before it, like so: `\\`.
So, let's say we wanted to represent the string:
And she said, "A backslash is \".
We would do it like this:
"And she said, \"A backslash is \\\"."
Of course, there's more escape sequences than `\\` and `\"`. Below is a list of escape sequences in JSON:
- `\b` - Backspace (U+0008)
- `\f` - Form Feed (U+000C)
- `\n` - Line Feed (starts a new line) (U+000A)
- `\r` - Carriage Return (U+000D)
- `\t` - Horizontal Tab (U+0009)
- `\uXXXX` - Where `XXXX` should be replaced with the hexadecimal number corresponding to a unicode character. For example, `\u3053` will result in `こ` (HIRAGANA LETTER KO) being read.
1.3. Booleans
A /boolean/ is a value that can be either `true` or `false`. They may be used to represent anything that has two possible states, such as whether a certain feature is enabled or not.
1.4. Null
`null` is a special value that represents the absence of a value. This is typically used as a placeholder, in order to signal something along the lines of "something should be here but isn't".
1.5. Arrays
An array is a sequence of values. This sequence is enclosed in brackets (`[]`) and every element in this sequence /must/ be separated by a comma. As well as that, commas /must not/ be placed anywhere else besides between multiple elements. JSON is generally very strict with where different characters should go, and violating these rules will cause an application that uses JSON to reject your data.
Arrays are used whenever you need a list of something, for example it could be used in a list of users on a website or a list of objects in a scene.
Examples:
- `[1, 2, 3]` - A list containing the numbers `1`, `2`, and `3`.
- `["abc", 2.5, true]` - Arrays may mix different values together; this array contains a string, a number, and a boolean.
- `["foo", [1, 2, 3], "bar", [4, 5, 6]]` - Arrays may even contain other arrays.
- `[]` - Arrays can also have zero elements in them.
- `[ 5, "foo",true,false]` - Whitespace is entirely ignored, so you may space arrays however you want.
Examples of invalid arrays:
- `["foo" "bar"]` - This array is missing a comma, so it will be rejected.
- `[1, 2, 3,]` - This array has an extra comma at the end, so it will be rejected. Note that, while obvious here, this can be hard to spot when the array spans multiple lines.
1.6. Objects
An object is a sequence of pairs, enclosed in braces `{}`. Each pair contains a /key/, and a /value/. Keys must be unique; there must not exist two pairs inside an object whose keys are equal. Values however, do not need to be unique. The key and value are separated from eachother with a colon (`:`), and the key /must/ be a string. The pairs themselves are separated from eachother via commas. Like arrays, the rules are very stringent: commas /must/ only be placed between pairs, and colons /must/ only be placed between the key and the value. If either of these rules are violated, then the JSON will be rejected.
Objects are used for things where a named list of properties make sense. For example, we could represent a person with the following JSON object:
{
"firstName": "John",
"lastName": "Doe",
"age": "31",
"location": "Washington, DC",
"hobbies": ["Programming", "Baking"]
}
Other examples:
- `{"foo": 1, "bar": 2}` - An object with the property `"foo"` as 1 and `"bar"` as 2.
- `{"obj": { "foo": [1, 2] } }` - Objects may contain other objects and arrays.
- `{}` - Empty objects are also allowed.
Invalid examples:
- `{1: 2, "foo": 3}` - The key `1` is not a string, so this is not allowed.
- `{"schrödinger": true, "schrödinger": false}` - The key `"schrödinger"` is repeated more than once, making this JSON invalid.
2. Playground
Below is a playground where you can expriment with writing JSON. Once you've written some JSON, you can press "Validate" and it will look for any errors in the JSON you've written and tell you in a human-friendly way.
[TODO: actually make the playground]
Footnotes
- * ...sort of. Depending on the programming language, /string/ can refer to a lot of different things; some languages use them for any binary data, others use them for strictly human-readable text. In the context of JSON however, they're virtually always used for text.
- † Except for control characters (U+0000-U+001F).
References
- [1] JSON.org, "JSON" www.json.org, 2023. https://www.json.org/json-en.html