What is JSON?
JavaScript Object Notation (JSON) is a text format for representing structured data based on the syntax of the JavaScript language. JSON file names use the .json file extension. JSON is usually used for data exchange between applications written in many programming languages, including JavaScript, Python, PHP, C++, Java, C#, Go, and many more.
JSON supports the following data types:
- Objects {...}
- Arrays [...]
- Primitives: string, numbers, boolean values true/false, null
JavaScript JSON.stringify() Method
The JSON.stringify(value, replacer, space) method converts JavaScript objects to a JSON string. The resulting JSON string is a JSON-formatted or serialized object that can be sent over the network or stored on a disk. You can restore or deserialize a JavaScript object from a JSON data string using the JSON.parse(text, reviver) method. Note that a JSON object has several important differences from literal objects: strings only use double quotes; object property names are enclosed in double-quotes.
JSON.stringify() Syntax
The following is a syntax for the JSON.stringify() method:
Where:
- value: a JavaScript object or value
- replacer (optional): a function that changes the behavior of the serialization process or an array of property names to be serialized
- space (optional): a string or numeric value used to insert spaces into the output JSON string for readability
JSON.stringify() Example
The following is a basic JSON.stringify() example:
JavaScript JSON.stringify() Examples
The following are examples of using the JSON.stringify() method in JavaScript:
Customizing the JSON.stringify() process
In most cases, the JSON.stringify() method is only used with the default parameters. If you need to customize the serialization process, such as filtering out circular references, you can do so with a "replacer" array that can be passed as the second argument to the JSON.stringify() method. For example, if you pass in an array containing property names, only those properties will be stringified.
Creating a pretty JSON string using JSON.stringify()
The third parameter of the JSON.stringify(value, replacer, space) method is the number of spaces used for convenient formatting. Objects serialized in JSON format have no indentation or extra space by default. This is useful if you send a JSON data string over the network or store it on the disk. The third "space" argument is used solely to produce human-readable output.
Creating a JSON String using the toJSON() method
JavaScript objects can provide an object.toJSON() method to control the serialization process. JSON.stringify() will automatically call this method if it exists for the object being serialized. The object.toJSON() method is used when calling JSON.stringify() directly on the object and when the object is nested within another serializable object.
How to decode JSON string back to JavaScript object?
To decode or parse a JSON data string back into a JavaScript object, you can use the JSON.parse() method: