I was trying to find a link to some simple examples of how to format JSON, but I couldn’t find what I was looking for. So I am making my own examples below. They are based on the documentation. You can format and validate JSON here.
Object
- Objects are surrounded with
{ }
curly braces. - Elements in the object are key-value pairs (name-value pairs).
- Keys must be quoted with
"
double quotes. - Keys and values are separated by a
:
colon. - Multiple key-value pairs are separated by
,
commas. - Key-value pairs are unordered.
- White space doesn’t matter.
{
"name" : "Mary",
"age" : 30
}
Array
- Arrays are surrounded with
[ ]
square brackets. - Elements of the array are values.
- Multiple values are separated by
,
commas. - Array values are ordered.
{
"animals" : [
"horse",
"cow",
"camel",
"sheep",
"goat"
]
}
In the example above, "animals"
is a key (name) and the [...]
array is a value.
Value
- Values can be strings, numbers, objects, arrays, boolean
true
orfalse
, ornull
.
{
"myString" : "Hello world", "myNumber" : 42,