Member-only story
Exploring JSON, JSON5, and Circular References
An in-depth guide on JavaScript Object Notation (JSON)
Object toString Problem
In JavaScript and TypeScript, an object is a collection of properties. For example:
const value = {a: 1, b: 2};
The value can be logged:
console.log(value); // {a: 1, b: 2}
How about a composed message?
console.log(`My value is ${value}`); // My value is [object Object]
Ops!
What is [object Object]
? It is the object’s toString()
value.
console.log
message can be fixed by suppling objects as independent parameters.
console.log('My value is', value); // My value is {a: 1, b: 2}
However, there are other cases that serialize objects using toString()
. For example, a JSX element:
<div>{value}</div>
In many situations, JSON.stringify()
is the rescuer.
We are going to take a close look at JavaScript Object Notation (JSON).
What is JSON?
JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write, and it is also 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.
JSON was specified first by Douglas Crockford in March 2001. It is a syntax for serializing objects, arrays, numbers, strings, booleans, and null
. It became an ECMA international standard in October 2013.
JSON has two main functionalities:
- It is a data format transferred between the client and the server.
- It is used to define configurations.