What is JSON?

JSON stands for JavaScript Object Notation. It’s a lightweight format for storing and transporting data similar to XML or YAML.

In this post, I’ll summarize everything you need to know about JSON in a simple describing language and then I’ll provide coding snippets on how you can send/receive JSON data using JavaScript.

JSON which stands for JavaScript Object Notation is lightweight, easy to read and write, and language-independent data interchange format. It is used widely across the internet for almost every single API as well as for config files and many other places. Even VS Code (Visual Studio Code) stores all of your configurations in a settings.json file.

JSON is based on a subset of the JavaScript Programming Language Standard ECMA-262 3rd Edition – December 1999. It is easy to read and write compared to something like XML because it has a much cleaner and simpler syntax. It is a text format that is completely language-independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, Java, etc.

JSON filenames have the extension of .json at the end.

JSON Syntax

  • Data is in name/value pairs
  • Data is separated by commas
  • Curly braces hold objects
  • Square brackets hold arrays
  • Keys & string typed values should be wrapped in double quotes

Look at the example below that how a common JSON object looks like:

 

 

 

 

 

 

Data types

Data types that can be used in JSON are:

  • Number
  • String
  • Null
  • Object (JSON Object)
  • Boolean
  • Array

The following data types are INVALID in JSON:

  • Function
  • Date
  • undefined

How to parse received JSON data?

Every language has its own API (Application Programming Interface) for sending/receiving JSON data. Let’s look at how JavaScript does it:

When receiving data from an API, that data is always a string. In order to use it, you should parse the data with the JSON.parse method and the data becomes a JavaScript object.

Look at the example below:

How to send JSON data to an API?

When sending data to an API or web server, the data has to be a string.

You can convert a JavaScript object using the JSON.stringify method into a string in order to send it to an API or a web server.

Look at the example below: