What is JSON Format?

JSON, also known as JavaScript Object Notation is a data representation format similar to XML or UML. Its most common usage is for APIs across the internet and configuration files for games and applications. The significant reasons for JSON’s popularity are its lightweight nature and because it’s straightforward to read and write, even to those who are ignorant about computer programming.

JSON is extremely popular in web development because it integrates easily with JavaScript, used throughout the web for front and backend applications. Also, almost every language has the built-in ability to parse JSON strings into objects or classes, making it extremely easy to use inside a programming language.

Origin of The JSON Data Type

In the early 2000s, the most common means of real-time server-to-browser communication was through browser plugins such as Flash or Java applets. JSON was created to meet the need for a stateless means of achieving this communication without the hassle of installing plugins.

The foundation for the growth of JSON began with Ajax capabilities allowing updates to a webpage without refreshing the page using Netscape’s built-in HTTP, HTML, and JavaScript support. 

Douglas Crockford, the founder of State Software and a respected JavaScript developer, was one of the leading proponents of the data format as a lightweight alternative to XML. He obtained the json.org domain name in 2002 and put his description of the format there; then, in 2006, he specified the format officially as RFC 4627.

JSON vs JavaScript

The most apparent relationship between JSON and JavaScript is that JavaScript is a part of JSON’s name. The name similarity is so because JSON uses JavaScript’s syntax and encoding to a large extent; anything you write in JSON is valid JavaScript.

JSON is popular because it eliminates the need for the reading between tags that XML makes users go through; this makes the data you send using JSON understandable to human eyes. 

There are two broad sets of data types in JavaScript; the first is primitive values like strings, numbers, and booleans. Also, another set of data types is known as objects, including objects, arrays, and dates. JavaScript reads JSON data as an object type, meaning you can apply methods meant for regular objects on JSONs.

const sampleJSON = {
	"firstName" : "Stephen",
	"lastName" : "Nabena"
}
console.log(typeof sampleJSON) // this value returned would be Object

JSON Syntax And Properties

JSON data is usually in a key/value pair similar to JavaScript objects; however, JSON keys must be strings, unlike objects. An example:

"firstName":"Stephen"

Data Types

While you can write JSON as plain text, there are several data types that it can collect to be interpreted correctly in other programming languages. Unsurprisingly, the data types that JSON accepts are similar to JavaScript data types; they are:

  • String— These are textual data enclosed in single or double-quotes.
  • Number— These are integer or decimal point floating numbers (since JavaScript doesn’t distinguish between the two). Numbers should not be within quotes, or they become strings.
  • Boolean— These are either true or false; again, these should not be within quotes, or they’ll become strings.
  • Null— These are empty or unassigned values represented using the word null (outside quotes).
  • Array— An array is a data type that holds a collection of zero or more items; these items could be of any data type. Square brackets should are used to enclose arrays, and a comma separates each item.
  • Object— Objects are a collection of items with a key/value pair. A colon separates the key/value pair; a comma separates each item, and curly braces enclose the entire object.

Example of JSON data containing various data types:

{
    "firstName" : "Stephen",
    "lastName" : "Nabena",
    "phoneNOs" : [
        2222233333,
        7899472849
    ],
    "married" : false,
    "children" : null,
    "otherInfo" : {
        "hobbies" : [
            "football",
            "singing"
        ],
        "job" : "writer"
    }
}

Exceptions and Limitations Of JSON

As flexible and popular as JSON is, there are exceptions and limitations to what you can do with it:

  1. You cannot parse date objects; they can only be written as strings and converted to date objects in the destination language.
  2. Parsing functions is impossible with JSON. While you can write them as strings and convert them to functions, that can make the function lose its scope.
  3. JSON doesn’t allow the addition of comments, leading to misunderstanding of some data objects.
  4. JSON data doesn’t have a schema, making it possible to create structureless data and cause users to run into errors.

JSON Files

JSON data can be created and kept in JavaScript code. However, you need to save JSON data in a .json file for use in other languages and transfer between users. JSON files are plain text files that you can open with every text editor, some word processors, and a notepad on Windows OS. They are almost as cross-platform compatible as .txt files, making them easy to parse in many programming languages.

Read More: What is JSON-RPC in JavaScript

Leave a Comment