In this post, we are going to discuss the rules of JSON syntax, the different values that can be used inside the JSON and their syntax as well and the file type of JSON file. So, let’s get started:
JSON Syntax rules
- The data inside the JSON can only be present as “key-value” pairs
- The key-value pairs are always separated by a colon
- Each data element or key-value pair must be separated by a comma
- To use objects inside the value of the key-value use curly brackets
- To use arrays inside the value field of the key-value pair use the square bracket
"Name": " John Doe",
JSON Data | Explained
Now that we are familiar with the JSON syntax rules, let’s have a look at the JSON data. As mentioned in the rules as well, the data inside the JSON format can only be written in “key-value” pairs. These key-value pairs are often called the name-value pairs as well.
Data consists of a key that is always encapsulated with double quotation marks followed by a colon “:” and then the value field of the data. Remember: the value field has its syntax which we are going to cover as well.
Example of JSON Data
An example of JSON data is:
"city": "New York"
Another example would be:
JSON Values | Acceptable Type
In the JSON syntax, the value field of the key-value pair can be one of the following data types:
String, as already mentioned above, are always encapsulated with double quotation marks, even if the string is being used inside an Array value.
For the data type of Arrays, the value field is encapsulated with square brackets, For example:
"name": "John Doe",
"Hobbies": ["Cricket", "Soccer", "Guitar", "Watching Netflix"]
An object in the value field is encapsulated with curly brackets. And you can even use nested arrays and objects, For example:
"name": "Bruce Wayne",
"Super Power": "Super Rich",
"Cars Owned": [
"Model": "LP 640",
"Model": " LP 700-4",
"Status": "Seems Driveable after the accident"
JSON | File Type
JSON data is often placed in a specific file which is known as the JSON file, this JSON file has the extension of “.json”. The media type of the file is “application/json”