What is JSON (JavaScript Object Notation)?

What-is-JSON-feature-image.jpg

In today’s world, data is everything, also storing and transmitting this data is important. This is the place where JSON comes. JSON stands for JavaScript Object Notation. It is one of the popular ways to store and transfer data on the internet. It’s lightweight and offers simple syntax, which is easily readable by humans and machines. Whether you are building a website or working with APIs, you will definitely use JSON. In this blog, you will learn everything about JSON, including why it is useful for developers and the best practices with examples.

Table of Contents:

What is JSON?

JSON stands for JavaScript Object Notation. It is defined as a way that is used to store and share data in a format that is easily understood by both humans and computers. It can store values in the form of key-value pairs. Here is the syntax for writing JSON data.

{
"name": "Intellipaat",
"year": 2008
}

Here, name and year are the keys, and Intellipaat and 2008 are the values for the keys. JSON originated from JavaScript, but now it is used in every programming language, like Python, Java, and PHP.

Data Types in JSON

JSON supports different types of data. These data types are used to store and organize information. Understanding these JSON data types helps you while parsing JSON data. Here are the following data types supported by JSON.

1. String

A string in JSON is defined as a sequence of characters that is enclosed in double quotes. It can contain numbers, letters, symbols, and spaces, and is commonly used to represent messages, names, or any other text data.

Syntax:

"name": "Intellipaat"

2. Number

A number is another JSON data type, which represents numeric values. Unlike strings, numbers in JSON are written without quotes and used to represent age, price, quantity, etc.

Syntax:

"age": 30

3. Boolean

Boolean data type represents only two states. One is true and the other is false. These values are useful for evaluating conditions, where you have to choose yes or no.

Syntax:

"isLearner": true
Design and Build Responsive Websites
Join Today
quiz-icon

4. Object

A JSON object is defined as a collection of key-value pairs that are wrapped inside the curly braces ( {} ). The key is always a string, and the value can be anything, like a string, a number, or even another object.

Syntax:

{
"name": "Intellipaat",
"year": 2008
}

5. Array

An array is defined as a list of values enclosed in square brackets ( [ ] ). The values can be anything, including strings, numbers, objects, or arrays.

Syntax:

"courses": ["AI", "Web Development", "App Development"]

6. Null

The null data type is a special data type that represents an empty or unknown value. It’s used when the field has no value.

Syntax:

"middleName": null

Examples of JSON

Let’s see some examples that will help you understand more about the JavaScript Object Notation (JSON):

Example 1: JSON with an Array

{
"name": "Intellipaat",
"learners": ["L1", "L2", "L3"]
}

In this example, a JSON object is created with two key-value pairs. The name key with a string value “Intellipaat” and the learners key with an array of items “L1”, “L2”, “L3”.

Example 2: Nested JSON Objects

{
"name": "L1",
"contact": {
"email": "[email protected]",
"phone": "9876543210"
}
}

You can also create one object inside another object, which is something called nested JSON objects. In this example, you are creating an object, and inside that object “contact” key holds another JSON object.

Example 3: JSON Array of Objects

{
"Learners": [
{
"name": "L1",
"age": "20"
},
{
"name": "L2",
"age": "26"
},
{
"name": "L3",
"age": "30"
}
]
}

In this example, you are creating a “Learners” array, which contains the details about the learner. Each learner stores two keys, one is name and the other is age. This format is commonly used in APIs.

Why JSON?

JavaScript Object Notation (JSON) is popularly used by developers to store and transfer data between applications. Here are some of the most popular reasons why JSON is used:

  1. JSON offers a simple and easy syntax, due to which a beginner who doesn’t have much knowledge about JSON can easily understand the lines.
  2. JSON does not have extra code because of which makes it the best choice to send and receive data over the internet.
  3. JSON mainly comes from JavaScript, but now it is used with different programming languages like Python, Java, PHP, and more.
  4. JSON is one of the popular formats, which is used to send and receive data in web applications.
  5. It provides support for complex data types like arrays and objects.

Common Use Cases for JSON

JSON is used in almost every application where the developer wants to send or receive data. Here are some important use cases for JSON:

  1. It is used in APIs and Web applications, where you need to share information among multiple applications.
  2. Web applications like an e-commerce site or a social media website use JSON to handle data like user details and product information.
  3. Some databases like MongoDB use a JSON-style format to store data.
  4. Your browser also sends the data and website information to the server in the form of JSON.
  5. Developers use tools like online JSON editors, JSON formatters, and JSON validators to view, clean, and check data.

What is JSON Schema?

A JSON schema is like a blueprint of a JSON file. You can imagine it as a rulebook that tells you about what kind of data is required, what keys should be there, and the data type of each value. A JSON schema will be helpful in validating your JSON data. For example, if your JSON data represents a user profile, the schema tells you exactly what keys are expected (like name, age, or email), what type of data each key should hold, and whether they are optional or required.

Why Use JSON Schema?

The reasons why a JSON schema may be used are:

  1. API Validation: It helps to validate your JSON object data before transmission or storage. Avoiding similar situations, an API that expects a number age, but receives a string, the schema will invalidate it.
  2. Data Contracts: The schema also avoids confusion and data structure mismatch in the occurrence of data sharing between different teams or data services, as all of the teams are under the same rules.
  3. API Design and Documentation: API design in the case of REST API design is described by the use of JSON Schemas that give the appearance of the request and response data. The majority of tools, including Swagger (OpenAPI), utilize JSON Schema to create API documentation and validation rules automatically.
  4. Error Prevention: JSON schema reduces binary run-time errors by the use of missing or invalid data through structure and types.

For example, consider the following JSON schema.

{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "Student Profile",
"type": "object",
"properties": {
"name": { "type": "string" },
"age": { "type": "integer", "minimum": 0 },
"email": { "type": "string", "format": "email" },
"isStudent": { "type": "boolean" },
"subjects": {
"type": "array",
"items": { "type": "string" },
"minItems": 1
},
"address": {
"type": "object",
"properties": {
"street": { "type": "string" },
"city": { "type": "string" },
"zipCode": { "type": "string" }
},
"required": ["street", "city"]
}
},
"required": ["name", "email", "subjects"]
}

In the above example, 

  • $schema defines the version of JSON Schema that is being used.
  • The title is just a name for the schema.
  • age must be a non-negative integer.
  • The email should follow the standard email format.
  • subjects is an array where each item must be a string, and it must contain at least one subject.
  • The address is a nested object with its own required fields (street and city).
  • The main object requires the fields name, email, and subjects to be present.

Parsing JSON Data Using JavaScript

While working with JSON, you may find a need to convert JSON into a JavaScript object, and this process is called Parsing. There are multiple ways to convert JSON into a JavaScript object, but using the JSON.parse() method in JavaScript is considered an easy and effective way to parse JSON data. JSON.parse() takes a JSON string and converts it into a JavaScript object that you can use in your code.

Example: Converting JSON into JavaScript objects.

Javascript

Output:

Converting JSON into JavaScript objects.

Learn how to parse JSON data in Java easily through this blog.

Working with JSON becomes easier if you’re using the right tools. Here are some important tools that will be used by developers while working with JSON data:

1. JSON Formatter

A JSON formatter helps in formatting JSON data and makes it easy to read for users. It organizes the data in a clean and structured format.

2. Online JSON Editor

An online JSON editor allows you to write, edit, and test JSON data directly in your browser. It helps in testing ideas quickly.

3. JSON Validator

A JSON validator is defined as a tool that checks whether your JSON data is correctly written or not. Making a small mistake in a JSON file, like a missing comma or an extra bracket, can break the code flow. That’s why using a JSON validator is important to find bugs in the code.

Best Practices for Using JSON

Using JSON is easy, and here are some best practices that you need to follow while using JSON data:

  1. Try to use double quotes (” “) for writing both the keys and the values in your JSON file.
  2. Validate your JSON data using a JSON validator always before using it in the code or APIs.
  3. Use a JSON formatter to format JSON data helps you to organize your code.
  4. JSON doesn’t support comments. Thus, if you want to add a comment, then write it in a separate key like “note”.

JSON VS XML VS YAML

Feature JSON XML YAML
Full Form JavaScript Object Notation Extensible Markup Language YAML Ain’t Markup Language
Main Use Data exchange between systems Data storage and document structure Configuration and setup files
Readability Easy to read, but has curly brackets and commas Harder to read due to the long opening and closing tags Very clean and human-readable
Data Types Has strings, numbers, booleans, arrays, and objects Everything is text (no specific data types) Has strings, numbers, booleans, lists, and maps
Comments Does not support comments Supports comments (<!– comment –>) Supports comments (# comment)
Parsing Speed Fast and lightweight Slower because of complex tags Moderate, depends on indentation
Common Uses Web APIs, configuration files, mobile apps SOAP web services, RSS feeds, document formats Configuration files for DevOps tools like Docker and Kubernetes
File Extension .json .xml .yaml or .yml

Get 100% Hike!

Master Most in Demand Skills Now!

Conclusion

JSON stands for JavaScript Object Notation. It is one of the most widely used formats for storing and sharing data. It is lightweight and offers a simple syntax for users. Whether you’re building a website or working with APIs, understanding JavaScript Object Notation or JSON is important to organize your data.

To learn more about JavaScript, explore the JavaScript Interview Questions and subscribe to our Web Development Course.

What is JSON – FAQs

Q1. What does the JSON do?

JSON helps you to store data in a structured format. This allows you to share and transfer data between different applications.

Q2. Is JSON a coding language?

No, JSON is not a programming language. It is a format that is used to store and transfer data.

Q3. Why is JSON very popular?

It is popular because it’s easy to read and doesn’t have any extra code.

Q4. Who invented JSON?

Douglas Crockford invented JSON.

Q5. Is JSON the same as JavaScript?

No, JSON is not the same as JavaScript. JSON is a data format inspired by JavaScript syntax, but it is language-independent. You can use JSON with almost any programming language, not just JavaScript.

Q6. Can JSON store complex data?

Yes, JSON can store complex data by combining objects and arrays.

Q7. Is JSON case-sensitive?

Yes, JSON is case-sensitive. For example, “Name” and “name” are treated as two different keys.

About the Author

Software Developer | Technical Research Analyst Lead | Full Stack & Cloud Systems

Ayaan Alam is a skilled Software Developer and Technical Research Analyst Lead with 2 years of professional experience in Java, Python, and C++. With expertise in full-stack development, system design, and cloud computing, he consistently delivers high-quality, scalable solutions. Known for producing accurate and insightful technical content, Ayaan contributes valuable knowledge to the developer community.

Full Stack Developer Course Banner