JSON (JavaScript Object Notation) is a popular lightweight data exchange format for sending data between a server and a client, or across various systems.
JSON data is parsed and interpreted using a software component or library called a JSON parser. Through the JSON parsing process, a JSON string is converted into a structured format that is easy to modify and access programmatically. Developers may deal with JSON data in a systematic and effective way thanks to the availability of JSON parsers in a variety of programming languages and frameworks.
JSON can be in the following two structures:
- Arrays i.e. Ordered list of items /values
- Objects i.e. Collection of key-value pairs
JSON parser reads and writes the formatted JSON data. It is used for mapping the JSON Object entries or attributes and the JavaScript objects, array, string, boolean, Number, etc. It can be performed in two types:
- Mapping JSON types to Entries or Attributes
- Mapping Entries or Attributes to JSON types
Mapping JSON types to Entries or Attributes: JSON types are mapped in a way that the entries are the value and the attributes as the properties having that value. So the structures data remain the same and it is converted to the JavaScript objects
Mapping Entries or Attributes to JSON types: These Entries and attributes to JSON objects are converted as the Attribute are the object properties and entries are the property Values maintaining the structure of the data from one type to another.
JSON data when converted is the reciprocal i.e. It can be reformed back as the original data and object from the converted state. THe data remains the same only the representation or outer form is changed. Hence no data is lost and is used efficiently.
Importance of using JSON Parsing
- Developers can transform JSON data into usable objects or data structures in their preferred programming language by using JSON parsing.
- For managing APIs, obtaining data from databases, and processing data obtained from online services, JSON parsing is essential.
- Developers may extract and use the necessary data by accessing particular data pieces inside a JSON structure thanks to JSON parsing.
JSON Parsing Methods
- Using JSON.parse() method
- fetch data from APIs or local JSON files
Method 1: Using JSON.parse() Method
JSON.parse() is a function included in JavaScript supports JSON parsing. It transforms JSON text into a JavaScript object so that its attributes may be easily accessed.
Syntax:
JSON.parse(jsonString);
Parameters: It takes JavaScript String as a parameter to parse.
Uses:
- JSON.parse(): This method analyzes a JavaScript String and outputs an object to make its attributes accessible.
Example: The code example shows how to implement a JSON parser with JavaScript using JSON.parse()
:
JavaScript
// Creating a JavaScript object
const jsonString =
'{"name": "Geek", "age": 22, "city": "Delhi"}';
// Creting JSON object
const obj = JSON.parse(jsonString);
console.log(obj.name); // Output: Geek
console.log(obj.age); // Output: 22
console.log(obj.city); // Output: Delhi
Output:
Geek
22
Delhi
Method 2: Fetching Data from Local File
In this method, we wiil import local json file and output the data on console using JavaScript require method.
// data.json file
{
"data": [
{
"name": "GFG",
"description" : "A Computer Science portal!"
}
]
}
Example: In this method, we will use require method to import the local data.json file and display output.
JavaScript
const sample = require('./data.json');
console.log(sample.data[0]);
JavaScript
// data.json file
{
"data": [
{
"name": "GFG",
"description" : "A Computer Science portal!"
}
]
}
Output:
{ name: 'GFG', description: 'A Computer Science portal!' }
There are more method to read the json files that you can find here https://www.geeksforgeeks.org/read-json-file-using-javascript/
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q
15+ min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read