JSON file structure
JSON format uses curly brackets to mark objects. Unlike XML, JSON files do not contain header section. This means the files must be checked for syntax errors before being used.JSON format uses curly brackets to mark objects. Unlike XML, JSON files do not contain header section. This means the files must be checked for syntax errors before being used.
Each JSON entry consists of a name-value pair, as show below:
{ “element”: 5 }
Variable names must be be quoted similar to string values. In the above example a variable of number type is named “element” and has value of 5. If we were to surround the integer value 5 with quotes we would in effect change the value to a string value of “5”:
{ “element”: “5” }
This rule is not strict, however. Values of null, true, and false are not surrounded with quotation marks if they represent values, not character strings.
JSON data are typically more complex than the above examples. They usually contain objects created using basic data types. For example, below is an object that represents a rectangle:
{
“rectangle”: {
“width”: 5,
“height”: 3
}
}
The rectangle object has two properties: width and height. Typical JSON file may contain many such simple objects. Indentations are used to make the code easier to read and modify if needed. Name-value pairs are separated with comma characters, and array elements are contained within a square brackets.