Json in javascript | Get value from json object in javascript

Json in js | Get value from json object in javascript

JSON in javascript | Get value from  json object in javascript

What is JSON?

  • JSON stands for JavaScript Object Notation
  • JSON is a lightweight data-interchange format
  • JSON is language independent *
  • JSON is “self-describing” and easy to understand

The JSON syntax is derived from JavaScript object notation syntax but the JSON format is text only Code for reading and generating JSON data be written in any programming language. The JavaScript JSON is an acronym for JavaScript Object Notation. It provides a format for storing and transporting data. It is a lightweight human-readable collection of data that can be accessed logically.

 

Why Use JSON?

The JSON format is syntactically similar to the code for creating JavaScript objects. Because of this, a JavaScript program can easily convert JSON data into a JavaScript object.

Since the format is text only, JSON data can easily be sent between computers and used by any programming language.

JavaScript has a built-in function for converting JSON strings into JavaScript objects:

JSON.parse()

JavaScript also has a built-in function for converting an object into a JSON string:

 

JSON.stringify()

  • You can receive pure text from a server and use it as a JavaScript object.
  • You can send a JavaScript object to a server in pure text format.
  • You can work with data as JavaScript objects, with no complicated parsing and translations.

 

Points to remember

  • It generates and stores the data from user input.
  • It can transport the data from the server to the client to the server, and server to the server.
  • It can also build and verifying the data.

Because JSON is derived from JavaScript programming, it is a natural choice to use as a data format in JavaScript. JSON, short for JavaScript Object Notation, is usually pronounced like the name “Jason”

To learn more about JSON in general terms, read the “An Introduction to JSON” tutorial.

To begin thinking about where you may use JSON in your JavaScript programs, some general use cases of JSON include:

 

JSON Syntax

  1. While working with the .json file, the syntax will be like this:

{  

            “First_Name” :  “value”;  

             “Last_Name”: “value “;  

               }

 

  1. While working with JSON object in .js or .html file, the syntax will be like:

var varName ={  

               “First_Name” :  “value”;  

                “Last_Name”:  “value “;  

                   }

 

JavaScript JSON Methods

Let’s see the list of JavaScript JSON methods with their description.

 

Methods Description
JSON.parse() This method takes a JSON string and transforms it into a JavaScript object.
JSON.stringify() This method converts a JavaScript value (JSON object) to a JSON string representation.

 

JavaScript JSON Example

Let see an example to convert string  JSON format using parse() & stringify() method.

//JavaScript to illustrate JSON.parse() method.  

var j = ‘{“Name”:”vikram”,”Email”: “zys”, “CN”: “000222”}’;  

var data = JSON.parse(j);  

document.write(“Convert string in JSON format using parse() method<br>”);  

document.write(data.Email); //expected output: XYZ  

//JavaScript to illustrate JSON.stringify() methods.  

var j = {Name:”Krishna”,   

Email: “XYZ”, CN : 000222};  

var data = JSON.stringify(j);  

document.write(“<br>Convert string in JSON format using stringify()  method<br>”);  

document.write(data); //expected output: {“Name”:”vikram”,”Email”:”XYZ”,”CN”:0022} 

 

Output:

Convert string in JSON format using parse() method

XYZ

Convert string in JSON format using stringify() method

{“Name”:”Krishna”,”Email”:”XYZ”,”CN”:12345}

 

JSON.stringify

The JSON JavaScript Object Notation is a general format to represent values and objects. It is described as in RFC 4627 standard. Initially, it was made for JavaScript but many other languages have libraries to handle it as well. So it’s easy to use JSON for data exchange when the client uses JavaScript and the server is written on /PHP/Java/Whatever.

 

JavaScript provides methods:

  • stringify to convert objects JSON.
  • parse to convert JSON back into an object.

 

For instance, here we JSON.stringify a student:

let student = { name: ‘John’, age: 30, isAdmin: false, courses: [‘html’, ‘css’, ‘js’], wife: null}; let json = JSON.stringify(student); alert(typeof json); // we’ve got a string! alert(json);/* JSON-encoded object: { “name”: “John”, “age”: 30, “isAdmin”: false, “courses”: [“html”, “css”, “js”], “wife”: null } */

 

Related:

 

People also ask

What is JSON () in JavaScript,

How can I use JSON in JavaScript,

Is JSON a JavaScript library,

What is JSON array JavaScript.

Leave a Comment

Your email address will not be published. Required fields are marked *