Let's Learn JSON Together
JSON stands for JavaScript Object Notation
JSON is an open standard lightweight data-interchange format that uses human-readable text to store and transmit data. It is commonly used for transmitting data in web applications.
Why the name “JavaScript Object Notation”?
→ JSON uses the basic idea of javascript data structure. Its origin is based on how JavaScript objects work, so in that sense, it is related. Regardless of the fact that it has its origin from JavaScript, it is widely used across many Languages.
Structure of JSON
Write given data in JSON format
- John, Williams, 30
- Glory, Williams, 35
{
"Students": [
{
"firstName": "John",
"lastName": "Williams",
"age": 30
},
{
"firstName": "Glory",
"lastName": "Williams",
"age": 35
}
]
}
It is easier to manage, update, and also to search anything both by humans and programming languages.
JSON Structures
- A collection of key-value pair, i.e. Object
- An ordered list of values, i.e. Array
Rules of JSON
- Data is in key-value pairs
- Data is separated by commas (,)
- Curly braces {} hold objects
- Square brackets [] hold arrays
---------------------------------------------------------------------------------------------------------------
A key/value pair consists of a field name or key (in double quotes), followed by a colon, followed by a value
Example →
"firstName": "John"
- In JSON, keys must be strings, written with double quotes.
- In JSON, values can be of type
- a string
- a number
- an object (JSON object)
- an array
- a boolean
- Null
--------------------------------------------------------------------------------------------------------------
All data is separated by a comma(,)
Example →
"firstName": "John", "lastName": "Williams", "age": 30
---------------------------------------------------------------------------------------------------------------
Curly braces {} hold objects or we can say JSON objects are surrounded by curly braces {}
Example → This is one object.
{
"firstName": "John",
"lastName": "Williams",
"age": 30
}
There can be nested objects i.e. objects within the object and inside these objects JSON rules remains the same.
- Keys must be strings, and values must be a valid JSON data type (string, number, object, array, boolean or null).
- Keys and values are separated by a colon.
- Each key/value pair is separated by a comma.
Access an Object
To access the object values use dot(.) or square brackets [] in programming.
Example →
studentObject = { "firstName": "John", "lastName": "Williams", "age": 30 };
Using dot(.)
x = studentObject.firstName;
Using square brackets []
x = studentObject["firstName"];
---------------------------------------------------------------------------------------------------------------
Square brackets [] hold arrays OR we can say Arrays are surrounded by Square Brackets []
Example →
{
"Students": [
{
"firstName": "John",
"lastName": "Williams",
"age": 30
}
]
}
Access an Object from Array
To access the Object from Array, store the complete Students array in a variable say myStudents
student = myStudents.Students[0]
JSON Validator
Once JSON is created, you can beautify and validate it using different online tools like https://jsonlint.com/. In case of invalid JSON, validator will give you error.
What is JSON Path?
It is the location or address or a specific value or data in a JSON. JSONPath is a query language for JSON, similar to XPath for XML. JSONPath helps to parse JSON data. You can create a JSON Path manually or use tools like JSON Path Finder
JSON Path Example:
x.Students[0].firstName
What is Media type?
Media type or MIME-type is a label used to identify a type of data. It is used so the software can know how to handle the data. So when you send data, you also send its media type so that the server will know what type of data you are sending and can parse it accordingly.
JSON Schema
JSON Schema is a JSON Document that describes other JSON Documents. It defines the structure of a JSON message. It can be used to validate a JSON message or API request and response in JSON format. You can create JSON Schema manually or use tools like JSON to JSON Schema Converter
Sample JSON
{
"users": [
{
"name": "John",
"age": 25
}
]
}
JSON Schema for above Sample JSON
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"users": {
"type": "array",
"items": [
{
"type": "object",
"properties": {
"name": {
"type": "string"
},
"age": {
"type": "integer"
},
"required": [
"name",
"age"
]
}
]
}
},
"required": [
"users"
]
}
You can use this tool to validate your JSON against schema: https://www.jsonschemavalidator.net/
Key Points To Remember
- JSON stands for JavaScript Object Notation.
- JSON is a syntax or format for storing and exchanging data.
- JSON is language independent.
- JSON is like One universal language for communication between programming languages and applications
- Platforms or applications built over different languages can exchange data or information using JSON.
- The file type or file extension for JSON files is “.json”
- The Media type for JSON text is “application/json”
- You will find a function JSON.parse() in all languages that can communicate with JSON format
Really helpful and well explained!! 👍🏻
Well explained 👏