JSON (JavaScript Object Notation)

What is JSON (JavaScript Object Notation)?

JSON (JavaScript Object Notation) is a text-based, human-readable data interchange format used to exchange data between web clients and web servers. The format defines a set of structuring rules for the representation of structured data. JSON is used as an alternative to Extensible Markup Language (XML).

Why is JSON used?

JSON is used in JavaScript on the internet as an alternative to XML for organizing data. JSON is language-independent and can be combined with C++, Java, Python and many other languages. Unlike XML, which is a full markup language, JSON is simply a way to represent data structures. JSON documents are relatively lightweight and are rapidly executed on web servers.

A JSON example

JSON consists of arrays and objects, as well as names and value pairs. Punctuation used in the format includes quotations, brackets, parentheses, semicolons and colons.

Data in JSON is written in name and value pairs, similar to JavaScript object properties. A name and value pair is constructed using a name that is placed in double quotes, followed by a colon and a given value.

"employees":[
  {"firstname":"John", "lastname":"Doe"},
     {"firstname":"Jane", "lastname":"Doe"},
 ]

JSON objects

JSON objects are unordered sets of name and value pairs. Objects are written inside of curly braces, like these { }. Everything inside the curly braces is part of the object. Objects can contain multiple name and value pairs. Each name is followed by a colon, and name value pairs are separated by a comma.

JSON arrays

Arrays in JSON are surrounded by square brackets, like these [ ]. Each value in the array is separated by a comma. Users can access array values and update, delete or loop them. An array can be stored inside another JSON array; that’s called a multidimensional array.

JSON conversions between text and object

There are two methods for converting between text and objects: parse() and stringify(). These methods might be used to read data from a web server when a developer has a JSON string and wants to convert it to an object. They can also be used when a user has a JavaScript object to send across a network that must be first converted to JSON.

Parse()

This method accepts a JSON string as a parameter and automatically returns a JavaScript object. To use parse(), create a JavaScript string that contains JSON syntax, and then use the function JSON.parse() to convert the string to a JavaScript object.

Stringify()

This method accepts an object as a parameter and automatically returns a JSON string. To use stringify(),create a JavaScript object, and then convert it using the stringify() function. After this, save the new value in a new variable.

Related Posts

What are custom events in JavaScript?

Custom events are the events that allow you to decouple the code you want to run after a specific piece of code runs. There are various in-built events…

How to use nested for loop in JavaScript?

We use the for loop statement of JavaScript for repeating a set of statements inside the loop body a specified number of times. A nested for loop, as the…

What are the basic rules for JavaScript parameters?

A JavaScript function is a code that performs a particular task. The function parameters are the name list in the function definition. Parameters are also known as…

How to stop refreshing the page on submit in JavaScript?

Using event.preventDefault() to stop page refresh on form submit In this section, we will see how to use event.preventDefault() to stop page refresh on form submission. The event.preventDefault() restricts the default…

Target a Window Using JavaScript or HTML

TARGET attribute of HTML A link’s opening named frame or window is specified using the Target attribute of the <a> anchor tag. The concluding </a> tag in…

What is the role of deferred scripts in JavaScript?

Since JavaScript is a loosely typed language, you are not required to correctly predict the kind of data that will be kept in a variable. Depending on the information…

Leave a Reply

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