- Tech know how online

JavaScript object notation (Ajax) (JSON)

The JavaScript Object Notation (JSON) is a compact format for the exchange of data, which was defined by Douglas Crockford. It is based on a subset of the JavaScript programming language - specifically, the JavaScript notation for object literals - according to the ECMA-262 standard (Third Edition, 1999). Nevertheless, JSON is an approach that structures data in a language-independent manner in a simple way, and it has since been adopted for many other programming languages such as Java, programming language C, C++, and many others.

Thus JSON can be used, in order to exchange data between programs, which were implemented in different programming languages. One speaks in this connection also of the serialization of data. JSON is a text format that can be read by both humans and machines. The great advantage of JSON lies in both its ease of use and implementation.

The elements of JSON

JSON distinguishes elements like objects, arrays, strings, numbers, boolean values (true and false) as well as the special value 0. Before or after each element there can be spaces, tabs, carriage returns and newline characters, in sum so-called whitespaces. The extensive use of whitespaces must be checked with regard to the aspects of transmission or storage costs.

A child container (a summary of different data) with key/value pairs defines a JSON object. A value can be any JSON value, including an array or object. The nesting depth of JSON objects can be arbitrarily deep; however, it is generally more effective to keep it relatively shallow. The structure of the JSON syntax is kept very simple, so most programming languages have a feature that easily maps JSON objects. This can be an object, struct, record, dictionary, hash table, list of properties, or associative array.

The concept of JSON

Example of JSON

Example of JSON

The real idea behind JSON is that these strings (object literals) can be transformed back into JavaScript arrays or objects very easily. Since JSON itself represents valid JavaScript code, the string can be executed directly and thus transformed into a JavaScript object. However, if the string is transformed using JavaScript's predefined eval() function, arbitrary JavaScript code can be executed, and thus unsafe code can be sprinkled into the actual application. However, this danger can be avoided by using the JSON.parse method for transformation (json2.js) instead of eval(). Alternatively, a framework such as mootools can be used for the use of JSON in JavaScript, which offers clean functions for the analysis of data structures(parsing) and for the conversion of objects to JSON.

The frequently brought up comparison of XML and JSON is invalid, since XML is a comfortable markup language with considerably more applications than the rather simple data exchange format JSON. Both have in common that both XML and JSON do not support the exchange of binary data. The more compact variant of JSON, namely BISON (Binary Interchange Standard Object Notation), offers itself here. Another method is BERT, which is based on Erlang.

Another technology is YAML (Yet another Markup Language); also a markup language for the serialization of data. This integrates JSON completely, so that a JSON format always also represents a valid YAML format.

Englisch: JavaScript object notation (Ajax) - JSON
Updated at: 01.11.2013
#Words: 525
Links: JavaScript, data, programming language (PL), notation, object (O)
Translations: DE

All rights reserved DATACOM Buchverlag GmbH © 2024