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.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

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