javascript - JSON - JSON File - JSON object

Modeling Data inside Javascript with JSON

JSON (Javascript Object Notation) is a human-readable data format that you can create in a simple text editor, no complex tools required. It was designed for easy handling within Javascript, but other programming languages can also interpret it easily.

JSON Structure

Data Types

JSON can hold information in six basic data types:

  • String: Zero or more Unicode characters surrounded by quotation marks.
  • Number: Whole or decimal number. It may be signed, may contain a fractional part, and may use exponential E notation.
  • Boolean: True or false value. They are specified by the words “true” or “false.”
  • Null: An empty value. This is specified by the word null.
  • Object: Collection of key-value pairs. Objects are declared with curly braces. Keys and values are separated by colons, while key-value pairs are separated by commas. Every key within an object must be unique.
  • Array: Ordered list of zero or more elements. It can include values of any data type, including null values, objects, and duplicates. Arrays are declared with square brackets and elements are separated by commas.

Data Pairing

JSON is built on the concept of creating data in pairs. The first part of a pair is called the “key,” and the second part of a pair is called the “value.” JSON data pairs are commonly referred to as “key-value pairs.”

The key in a JSON key-value pair must be a string containing one or more Unicode characters surrounded by quotation marks. You should avoid non-informative characters like emojis, non-printable characters, and extra whitespace.

The value in a JSON key-value pair may be any valid JSON data type. If the value is a string, backslash escapes for non-printable or special characters are allowed.

Data as an Object

All JSON data must be part of a JSON object. A JSON file must contain only one JSON object. JSON objects are similar to Javascript objects except that they cannot hold some of Javascript’s complex data structures, like functions.

Whitespace is only significant within a string. You can add as much whitespace as you wish around array elements, key-value pairs, object curly braces, or other data.

JSON does not allow comments and cannot hold binary data within objects.

JSON File Example

This example shows a valid JSON file with a single root object and key-value pairs containing every type of valid value: number, string, true, false, null, array, and object.

    “Key1” : 1,
    “Key2” : “a”,
    “Key3” : true,
    “Key4” : false,
    “Key5” : null,
    “Key6” : [1, 2, 3],
    “Key7” : {
        “Key8” : 2,
        “Key9” : “b c”

Handling JSON Files

Interpreting JSON Data

When working with JSON inside a Javascript program, a JSON object is always read into a string. To get keys and values from the string, a program called a parser splits the JSON into keys and values, then converts them into a Javascript object.

You can write a JSON parser in any programming language you wish, but JSON is so popular and widely used that you don’t have to reinvent the wheel. Every popular programming language already has at least one well-maintained JSON parser.

Some programming languages, like C++ and Java, have over a dozen reliable parsers to choose from. The official JSON website,, keeps an updated list containing hundreds of reliable parsers written in more than 50 programming languages.

When to Use JSON

JSON is a common way to deliver data in web applications. It can hold data within Javascript web scripts and in complex data delivery systems like REST APIs.

Since JSON cannot hold binary data, it cannot hold many common data types like audio, video, or images. If you want to include references to these data in a JSON file, use a string to specify a hyperlink address or file path.


JSON is a simple and reliable data format that is commonly used in Javascript programs and also has a place in general data exchange and modeling. Its simplicity contributes to its popularity, but it’s not useful for all types of data, and large JSON files can become difficult to handle.

Enroll in our Intro to Programming Nanodegree program today to learn more about JSON and other programming concepts.

Start Learning

Jessica Reuter Castrogiovanni
Jessica Reuter Castrogiovanni