JSON

An overview of JavaScript Object Notation and using it in Node.js.

Scroll down...

Content

Resources

Comments

This lesson covers what JSON is and why you should care about it. Further, it discusses how it is used in Node.js and some simple native features that make JSON a pleasure to work with in Node.js.

What is JSON?

JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999.

You've most likely heard of JSON. You may have experience using it and writing it as well. If you've written out a JavaScript object literal (which you have) you've basically written JSON. JSON is inspired by JavaScript's object literal syntax:

{
  "en": "Hello world!",
  "es": "Hola mundo!"
}

The main difference between JSON and JavaScript is that JSON is only data. It is not JavaScript code. You don't execute functions or expressions in JSON and there are no JSON comments. JSON is just for representing data in specific types.

Here we can see the supported data types in JSON:

  • Number
  • Boolean
  • Array
  • String
  • Object
  • null

It is also worth noting that JSON keys must be wrapped with double quotes "". It is part of the specification and avoids clashes with reserved JavaScript keywords like for and switch.

How to Use JSON in Node.js

Using JSON in Node.js couldn't be easier. Let's say for example that you have a file called salutations.json. This is a JSON file. It has the extension .json. The require method in Node.js is setup to require .json files by default. Not only that, but it will actually parse the JSON data and return it as a ready-to-use JavaScript object! Let's see this in action.

Let's say this is our JSON file at the path ./lib/salutations.json:

{
  "en": "Hello world!",
  "es": "Hola mundo!"
}

Now here is how we can require and use it:

var salutations = require('./lib/salutations');

console.log(salutations.en);
//=> "Hello world!"

console.log(salutations.es);
//=> "Hola mundo!"

The require function parsed the JSON file into a usable JavaScript object! Not only that, but it did so without specifying the file extension in the path. You can imagine how useful this becomes when working with JSON files.

Code Review

Here are the important snippets of code from this lesson.

{
  "en": "Hello world!",
  "es": "Hola mundo!"
}
var salutations = require('./lib/salutations');

console.log(salutations.en);
//=> "Hello world!"

console.log(salutations.es);
//=> "Hola mundo!"

Wrapping Up

JSON is largely the format of data on the web these days. Understanding how to read JSON is a key skill for any web developer in ANY programming language. So much data and so many APIs use JSON that it is basically unavoidable. As you've learned, Node.js makes it super simple to work with and you'll benefit from this simplicity greatly as you move forward.



Sign up to track your progress for free

There are ( ) additional resources for this lesson. Check them out!

Sorry, comments aren't active just yet!

Next Lesson: Node.js Basics