The JavaScript Object Notation (JSON) is a lightweight text-based format for serializing and transmitting structured data over network connections.
It originates from the Javascript programming language, having been designed to be human-readable. You will see that JSON is easy to read and write.
Despite its Javascript roots, JSON can be easily used with many programming languages, including Java and Python.
{
"book": [
{
"id":"01",
"language": "Java",
"edition": "third",
"author": "Herbert Schildt"
},
{
"id":"07",
"language": "C++",
"edition": "second",
"author": "E.Balagurusamy"
}
]
}
JSON is widely used in web APIs:
Here are some open examples we can easily inspect.
JSON supports the following data structures.
A map, usually called an object in JSON, is represented with curly braces ({}
).
:
), and then a value.,
).{
"id": 1,
"language": "Java",
"edition": "third",
"author": "Herbert Schildt",
}
A list, usually called an array in JSON, is represented with square brackets ([]
).
,
).We can create lists of strings,
[ "My", "name", "is", "JSON" ]
lists of numbers,
[ 1, 100, 1000 ]
lists of objects,
[
{
"id": 1,
"language": "Java",
"edition": "third",
"author": "Herbert Schildt",
},
{
"id": 7,
"language": "C++",
"edition": "second",
"author": "E.Balagurusamy",
}
]
and even a mix of datatypes.
[1, "John", true, [0, 1], { "id": 1 }, null ]
JSON supports the following data types:
Type | Description |
---|---|
Number | double- precision floating-point format in JavaScript |
String | double-quoted Unicode with backslash escaping |
Boolean | true or false |
Array | an ordered sequence of values |
Value | a string, a number, true or false, null |
Object | an unordered collection of key:value pairs |
null | empty |
This JSON document...
{
"book": [
{
"id": "01",
"language": "Java",
"edition": "third",
"author": "Herbert Schildt"
},
{
"id": "07",
"language": "C++",
"edition": "second",
"author": "E.Balagurusamy"
}
]
}
... is equivalent to this XML document:
<?xml version="1.0" encoding="UTF-8" ?>
<book>
<id>01</id>
<language>Java</language>
<edition>third</edition>
<author>Herbert Schildt</author>
</book>
<book>
<id>07</id>
<language>C++</language>
<edition>second</edition>
<author>E.Balagurusamy</author>
</book>