JSON

From Canonica AI

Introduction

JSON, an acronym for JavaScript Object Notation, is a lightweight data-interchange format that is easy for humans to read and write and easy for machines to parse and generate. It is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.

History

JSON was popularized by Douglas Crockford, a well-known American computer programmer and entrepreneur who is involved in the development of the JavaScript language. He discovered JSON and its potential to revolutionize data interchange on the web, and he defined it on the JSON.org website in 2002. The JSON format was originally specified and developed by two programmers, Douglas Crockford and Chip Morningstar.

A screenshot of a JSON file with various data types.
A screenshot of a JSON file with various data types.

Syntax and Data Types

JSON's basic data types are:

- Number: a signed decimal number that may contain a fractional part and may use exponential E notation, but cannot include non-numbers such as NaN. The format makes no distinction between integer and floating-point. JavaScript uses a double-precision floating-point format for all its numeric values, but other languages implementing JSON may encode numbers differently.

- String: a sequence of zero or more Unicode characters. Strings are delimited with double-quotation marks and support a backslash escaping syntax.

- Boolean: either of the values true or false.

- Array: an ordered list of zero or more values, each of which may be of any type. Arrays use square bracket notation with elements comma-separated.

- Object: an unordered collection of key:value pairs where the keys are strings. Objects are intended to represent associative arrays, where each key is unique within an object. Objects are delimited with curly brackets and use commas to separate each key:value pair.

- null: an empty value, using the word null.

Use Cases

JSON is often used when data is sent from a server to a web page. The JSON format is used for serializing and transmitting structured data over network connection. It is primarily used to transmit data between a server and web applications, serving as an alternative to XML.

JSON vs XML

While JSON and XML can both be used to receive data from a web server, JSON is often preferred for its simplicity. Unlike XML, JSON does not require a parsing function and is faster to read and write. JSON is also less verbose, meaning it has a more compact style than XML, and is quicker for humans to read and write and for machines to parse and generate.

JSON in Web Services and APIs

JSON is often used in web services and APIs. When data is stored on the server or sent to the server, it must be in a string format. JSON is a string format that is as close to JavaScript objects as possible. This means that when data is delivered from the server to a web page, it can be immediately used as a JavaScript object.

JSON and JavaScript

While JSON is a subset of the JavaScript object literal syntax, they are not the same. JSON requires double quotes around the keys, while JavaScript does not. JSON also does not support JavaScript's NaN and Infinity values, instead it has a null value which JavaScript does not have.

JSON Parsing and Stringification

JSON data received from the server must be converted into a JavaScript object before it can be used. This process is called parsing. Conversely, when JavaScript objects need to be sent to a server, they must be converted into a JSON string. This process is called stringification.

JSON Schema

A JSON Schema defines the structure and the legal values of a JSON document. The JSON Schema is written within IETF draft which got expired in 2011. JSON Schema − Describes your existing data format. Clear, human- and machine-readable documentation. Complete structural validation, useful for automated testing.

See Also