The Wayback Machine - https://web.archive.org/web/20240917100907/https://www.geeksforgeeks.org/json/
Open In App

JSON Tutorial

Last Updated : 17 Jun, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

JSON stands for JavaScript Object Notation is a lightweight and human-readable format for storing and exchanging data. It is a format for structuring data. This format is used by different web applications to communicate with each other. It has become the actual standard for data communication across web applications due to its simplicity and flexibility.

In this JSON tutorial, we will provide you with the fundamentals of JSON, JSON Syntax including objects, arrays, values, keys, and string formats, JSON topics including parsing JSON in various programming languages, using JSON for web APIs, and data handling of large JSON datasets, enabling you to create, parse, and leverage JSON data effectively.

JSON Tutorial

What is JSON?

JSON, short for JavaScript Object Notation, makes sharing data simple and straightforward. Created by Douglas Crockford, it’s designed for easy reading and writing by humans, and easy parsing and generating by computers. Its main goal was to make a text format that’s good at showing simple data like lists and text, and really useful for websites.

JSON is special because it’s very clear and easy to use, and it uses a “.json” file ending to show that a file is in this format. This makes JSON great for both people and programs to work with.

Data Types that JSON supports:

According to the JSON standards, values in JSON must be one of the following data types:

  • Strings
  • Numbers
  • Object (JSON Object)
  • Array
  • Booleans
  • Null

Why use JSON?

JSON is used in a variety of contexts, primarily for data interchange between servers and web applications: Here are the reasons:

  • Language Independence : Though it is derived from a subset of JavaScript, yet it is Language independent . Thus, the code for generating and parsing JSON data can be written in any other programming language.
  • Human-readable Format : It is Human-readable and writable.
  • Lightweight Data Interchange Format : It is a lightweight text-based data interchange format which means, it is simpler to read and write when compared to XML.
  • Easy Parsing and Generation : Allows conversion of parse JSON data into native data structures and vice versa easily, simplifying the process of working with data.

JSON Syntax

In JSON , data is primarily stored in two structures: objects and arrays . Here’s a detailed breakdown of the syntax for each:

1. Using ‘Objects’

  • Objects in JSON are collections of key/value pairs enclosed in curly braces {} .
  • Each key is a string (enclosed in double quotes " ) followed by a colon : , and the key/value pairs are separated by commas (,) .
Example: {"firstName": "John", "lastName": "Doe", "age": 30}

2. Using ‘Arrays’

  • Arrays are ordered lists of values, enclosed in square brackets [] .
  • Values within an array are separated by commas (,) .
Example: ["apple", "banana", "cherry"]

JSON Example

data.json
"Data Structures": [
        {
            "Name" : "Trees",
            "Course" : "Intoduction of Trees",
            "Content" : [ "Binary Tree", "BST",
                        "Generic Tree"]
        },
        {
            "Name" : "Graphs",
            "Topics" : [ "BFS", "DFS", "Topological Sort" ]
        }
    ]
}

JSON Tutorial

Before getting started with JSON, you must have basic programming knowledge and familiarity with data structures like objects and arrays.

Useful JSON Tools

Advantages of JSON

  • Readability: JSON closely resembles JavaScript object syntax, making it easy for humans to understand and write.
  • Lightweight: Compared to XML, JSON has a smaller footprint, leading to faster data transmission and processing.
  • Language Independence: JSON is not tied to any specific programming language. Most programming languages have libraries to parse and generate JSON data.
  • Flexibility: JSON’s flexible structure can accommodate various data types and complex data hierarchies.

Limitations of JSON

  • The main limitation is that there is no error handling . If there was a slight mistake in the script then you will not get the structured data.
  • It becomes quite dangerous when you used it with some unauthorized browsers . Like JSON service return a JSON file wrapped in a function call that has to be executed by the browsers if the browsers are unauthorized then your data can be hacked.
  • It has limited supported tools that we can use during the development.

Working with JSON in Applications

JSON’s true power lies in its ability to seamlessly exchange data between applications. Here are some common use cases:

  • APIs (Application Programming Interfaces): APIs often use JSON to format data requests and responses. This makes it easier for different applications to communicate and share information.
  • Client-Server Communication: Web applications frequently use JSON to send data from the browser (client) to the server and vice versa. JSON’s lightweight nature ensures efficient data transfer.
  • Data Storage: JSON can be used to store configuration settings, user preferences, or any other structured data in a file.

Frequently Asked Questions About JSON

What is the full-form JSON?

“JSON” stands for ” JavaScript Object Notation ” and is a lightweight data format used for easy exchange of data between different systems.

What is JSON used for?

JSON is used for data interchange between servers and web applications, configuration files, and storing structured data.

What is the basic structure of JSON?

JSON structure is made of k ey-value pairs within objects {} and ordered lists of values within arrays [] .

What are the types of JSON?

In JSON, “types” typically refer to the values that can be represented within the JSON format. JSON supports the following data types:

  1. String: A sequence of characters, enclosed in double quotes. Example : "Hello, world!"
  2. Number: Integer or floating-point numbers. Example : 42 or 3.14
  3. Object: An unordered collection of key-value pairs, enclosed in curly braces {} . Example : {"name": "John", "age": 30}
  4. Array: An ordered list of values, enclosed in square brackets [] . Example : ["apple", "banana", "cherry"]
  5. Boolean: Represents true or false values. Example : true or false
  6. Null: Represents a null value, indicating the absence of any value. Example : null

Is JSON Slowing Down Our Apps?

In some case, especially for large data sets. JSON is easy to read but larger and requires more processing than some alternatives

Why is JSON preferred over XML?

JSON is more lightweight, easier to read and write, and faster to parse than XML.

What are the alternative of JSON?

The best choice depends on your needs: data size, readability, and development time. Here are some alternatives of json:

  • Protocol Buffers : Smaller, faster format, good for various languages.
  • MessagePack : Efficient for real-time data exchange.
  • Custom Binary Formats : Fastest option, but requires more development effort.


Similar Reads

Why does Vite create multiple TypeScript config files: tsconfig.json, tsconfig.app.json and tsconfig.node.json?
In Vite TypeScript projects you may have noticed three typescript configuration files, tsconfig.json, tsconfig.app.json, and tsconfig.node.json. Each file is used for a different purpose and understanding their use cases can help you manage TypeScript configurations for your project. In this article, you will learn about the three TypeScript config
6 min read
JSON Modify an Array Value of a JSON Object
The arrays in JSON (JavaScript Object Notation) are similar to arrays in JavaScript. Arrays in JSON can have values of the following types: nullbooleannumberstringarrayobject The arrays in JavaScript can have all these but it can also have other valid JavaScript expressions which are not allowed in JSON. The array value of a JSON object can be modi
2 min read
How to parse JSON object using JSON.stringify() in JavaScript ?
In this article, we will see how to parse a JSON object using the JSON.stringify function. The JSON.stringify() function is used for parsing JSON objects or converting them to strings, in both JavaScript and jQuery. We only need to pass the object as an argument to JSON.stringify() function. Syntax: JSON.stringify(object, replacer, space); Paramete
2 min read
How to transforms JSON object to pretty-printed JSON using Angular Pipe ?
JSON stands for JavaScript Object Notation. It is a format for structuring data. This format is used by different web applications to communicate with each other. In this article, we will see How to use pipe to transform a JSON object to pretty-printed JSON using Angular Pipe. It meaning that it will take a JSON object string and return it pretty-p
3 min read
What is difference between JSON.parse() and JSON.stringify() Methods in JavaScript ?
JSON.parse() converts JSON strings to JavaScript objects, while JSON.stringify() converts JavaScript objects to JSON strings. JavaScript utilizes JSON for data interchange between servers and web pages. These methods enable easy data manipulation and transport in web development. JSON.parse() MethodJSON.parse() converts a JSON string to a JavaScrip
2 min read
How to Write Postman Test to Compare the Response JSON against another JSON?
In the era of API-driven development, testing the API responses to ensure their correctness is of paramount importance. One such test includes comparing the JSON response of an API against a predefined JSON. In this article, we will delve into how to write a Postman test for this purpose. Table of Content What is Postman?JSON and its Role in APIsWh
4 min read
How to use cURL to Get JSON Data and Decode JSON Data in PHP ?
In this article, we are going to see how to use cURL to Get JSON data and Decode JSON data in PHP. cURL: It stands for Client URL.It is a command line tool for sending and getting files using URL syntax.cURL allows communicating with other servers using HTTP, FTP, Telnet, and more.Approach: We are going to fetch JSON data from one of free website,
2 min read
Convert JSON String to Array of JSON Objects in JavaScript
Given a JSON string, the task is to convert it into an array of JSON objects in JavaScript. This array will contain the values derived from the JSON string using JavaScript. There are two approaches to solving this problem, which are discussed below: Table of Content Using JSON.parse() MethodUsing JavaScript eval() MethodUsing Function ConstructorU
4 min read
Difference between package.json and package-lock.json files
In Node, package.json file contains the list of dependencies and scripts in a project while the package.lock.json specifies their respective versions to ensure consistent installations in different environments. In this article, we will learn the major differences between package.json and package.lock.json and their needs in Node. Table of Content
4 min read
How to make a JSON call using jQuery ?
Use the getJSON() function in jQuery to load JSON data. The getJSON() function uses a GET HTTP request to retrieve JSON-encoded data from the server. In this article, we will learn about the jQuery getJSON() function and its implementation through examples. Syntax: $(selector).getJSON(url, data, success(data, status, xhr)) Parameters: This method a
3 min read
How to generate Json File in PHP ?
In this article, we are going to generate a JSON file in PHP by using an array. JSON stands for JavaScript object notation, which is used for storing and exchanging data. JSON is text, written with JavaScript object notation. Structure: {"data":[ { "sub_data1":"value1", "sub_data2":"value2","sub_data_n":"value n" }, { "sub_data2":"value2","sub_data
3 min read
Javascript | JSON PHP
JSON stands for the JavaScript Object Notation. It is used to exchanging and storing the data from the web-server. JSON uses the object notation of JavaScript. JavaScript objects can be converted into the JSON and receive JSON format text into the JavaScript objects. Converting the JavaScript object into the JSON format is done by the given functio
7 min read
Google Geo-coding Web Service (JSON response)
Prerequisite : JSON Formatting in Python Google has an excellent web service that allows us to make use of their large database of geographic information. Here, we are going to be working with the Google Maps API. In the old days, this Maps API was free and did 2, 500 requests per day but now they've made it so that parts of it are behind API keys
4 min read
JavaScript | JSON HTML
HTML table: In the HTML, tables are defined by <table> tag, table's header are defined by <th> tag and by default table's header are placed in center and it is bold and row of the table is defined by <tr> and the data or information of the row is defined by <td> tag. Below code shows the demonstration of the HTML table: Code
3 min read
JavaScript | JSON Arrays
The JSON Arrays is similar to JavaScript Arrays. Syntax of Arrays in JSON Objects: // JSON Arrays Syntax { "name":"Peter parker", "heroName": "Spiderman", "friends" : ["Deadpool", "Hulk", "Wolverine"] } Accessing Array Values: The Array values can be accessed using the index of each element in an Array. C/C++ Code <!DOCTYPE html> <html>
2 min read
Convert Java Object to Json String using Jackson API
JSON stands for JavaScript Object Notation. It's a standard text-based format that shows structured data based on JavaScript object syntax. It is commonly used for transmitting data in web applications. JSON is highly recommended to transmit data between a server and web application. In order to convert a Java object into JSON, the following two me
3 min read
How to receive JSON POST with PHP ?
In this article, we will see how to retrieve the JSON POST with PHP, & will also see their implementation through the examples. First, we will look for the below 3 features: php://input: This is a read-only stream that allows us to read raw data from the request body. It returns all the raw data after the HTTP headers of the request, regardless
2 min read
Interesting facts about JSON
JSON or JavaScript Object Notation is a lightweight format for transporting and storing data. JSON is used when data is transferred from a server to a web page. This language is also characterized as weakly typed, prototype-based, dynamic, and multi-paradigm. Here are some interesting facts about JSON: JSON was derived from JavaScript but by 2017 m
2 min read
JavaScript JSON parse() Method
The JSON.parse() method in JavaScript is used to parse a JSON string which is written in a JSON format and returns a JavaScript object. Syntax: JSON.parse( string, function ) Parameters: This method accepts two parameters as mentioned above and described below: string: It is a required parameter and it contains a string that is written in JSON form
2 min read
Convert Java Object to Json String using GSON
JSON Stand for JavaScript Object Notation. It's a standard text-based format which shows structured data based on JavaScript object syntax. It is commonly used for transmitting data in web applications. JSON is highly recommended to transmit data between a server and web application. To convert a Java object into JSON, the following methods can be
2 min read
Convert Json String to Java Object Using GSON
Pre-requisite: Convert Java Object to Json String Using GSONJSON Stand for JavaScript Object Notation. It's a standard text-based format which shows structured data based on JavaScript object syntax. It is commonly used for transmitting data in web applications. JSON is highly recommended to transmit data between a server and web application. To co
3 min read
AngularJS json Filter
The json filter in AngularJs is used to convert a JavaScript object into a JSON. string.JavaScript object that we are using can be of any kind of JavaScript Object. The json filter piped the object or any expression with JSON so that the result will be displayed in the form of a list, which is bound with the expression syntax. Syntax: {{ object | j
2 min read
JavaScript | Remove a JSON attribute
In this article, we will see how to remove a JSON attribute from the JSON object. To do this, there are few of the mostly used techniques discussed. First delete property needs to be discussed. Delete property to Remove a JSON Attribute This keyword deletes a property from an object: This keyword deletes both the value of the property and the prope
2 min read
JavaScript | Convert an array to JSON
Here is a need to convert an array into a JSON_Object. To do so we are going to use a few of the most preferred techniques. First, we need to know a few methods. 1) Object.assign() method This method copies the values of all properties owned by enumerables from source objects(one or more) to a target object. Syntax: Object.assign(target, ...sources
2 min read
JavaScript | Add new attribute to JSON object
The task is to add a JSON attribute to the JSON object. To do so, Here are a few of the most used techniques discussed.Example 1: This example adds a prop_11 attribute to the myObj object via var key. C/C++ Code <!DOCTYPE HTML> <html> <head> <title> JavaScript | Add new attribute to JSON object. </title> </head>
2 min read
How to send a JSON object to a server using Javascript?
JavaScript Object Notation (JSON). It is a lightweight data transferring format. It is very easy to understand by human as well as machine. It is commonly used to send data from or to server. Nowadays it is widely used in API integration because of its advantages and simplicity.In this example we are going to use AJAX (Asynchronous JavaScript And X
2 min read
How to swap key and value of JSON element using JavaScript ?
In this article, we will swap the key and value of JSON elements using JavaScript. Given a JSON object and the task is to swap the JSON object key with values and vice-versa with the help of JavaScript. Below are the following approaches: Using for loopUsing Object.keys() and ForEach() MethodUsing Object.entries() and map() MethodApproach 1: Using
2 min read
How to convert JSON results into a date using JavaScript ?
The task is to convert a JSON result to JavaScript Date with the help of JavaScript. There are two methods which are discussed below: Approach 1: Use substr() method to get the integer part of the string.Use the parseInt() method followed by Date() to get the JavaScript date. Example: This example implements the above approach. C/C++ Code <h1 st
2 min read
How to universally parse JSON into blocks using jQuery ?
In jQuery, to parse any data to any block is carried over by using DOM Insertion methods. Some of DOM Insertion methods are append(), appendTo(), html(), prepend(), prependTo(), text(). To parse JSON into any block is also handled in same way but along with Ajax callback functions and parse.JSON() methods. Here parse.JSON() methods is deprecated in
3 min read
JSON full form
JSON stands for JavaScript Object Notation. It is a text-based data interchange format to maintain the structure of the data. JSON is the replacement of the XML data exchange format in JSON. It is easy to struct the data compare to XML. It supports data structures like arrays and objects and JSON documents that are rapidly executed on the server. I
3 min read