JSON (JavaScript Object Notation)
What is JSON (JavaScript Object Notation)?
JSON (JavaScript Object Notation) is a text-based, human-readable data interchange format used to exchange data between web clients and web servers. The format defines a set of structuring rules for the representation of structured data. JSON is used as an alternative to Extensible Markup Language (XML).
JSON was originally based on the JavaScript programming language and was introduced as the page scripting language for the Netscape Navigator web browser. JSON is also sometimes used in desktop and server-side programming environments.
American computer programmer Douglas Crockford created JSON. The format is derived from the JavaScript programming language standard and follows JavaScript object syntax. JSON consists of name-object pairs and punctuation in the form of brackets, parentheses, semicolons and colons. Each object is defined with an operator, such as text or image, and grouped with a value for that operator. JSON files are labeled as .json. JSON has a language-independent format.
JSON has a simple structure and doesn't use mathematical notation or algorithms. It is easy to understand, even by users with limited programming experience. It is seen as a quick, approachable way to create interactive pages. It has become the format of choice for publicly available web services. It has native support in relational and NoSQL databases.
New JSON users must be aware of potential security implications, however. JSON scripts automatically execute in any webpage that's requested by a web browser. Consequently, they can be used to implement JavaScript insertion attacks against a web client, like a command injection or cross-site scripting.
For example, if a hacker inserts non-JSON code into a string, such as a Trojan horse, the targeted algorithm executes the text as if it were JavaScript and then returns the value of the last statement. If the only statement is a JSON value, there's no effect. However, if a previous statement contains other JavaScript code, that code is executed by the script. This might give the hacker access to all the variables a script has access to, potentially compromising a user's PC.
Why is JSON used?
JSON was accepted as a standard by the International Organization for Standardization as ISO/IEC 21778:2017 with the goal of defining the syntax of valid JSON texts.
JSON is used in JavaScript on the internet as an alternative to XML for organizing data. JSON is language-independent and can be combined with C++, Java, Python and many other languages. Unlike XML, which is a full markup language, JSON is simply a way to represent data structures. JSON documents are relatively lightweight and are rapidly executed on web servers.
A JSON example
JSON consists of arrays and objects, as well as names and value pairs. Punctuation used in the format includes quotations, brackets, parentheses, semicolons and colons.
Data in JSON is written in name and value pairs, similar to JavaScript object properties. A name and value pair is constructed using a name that is placed in double quotes, followed by a colon and a given value.
For example, an array of employee names may look like this:
"employees":[
{"firstname":"John", "lastname":"Doe"},
{"firstname":"Jane", "lastname":"Doe"},
]
Each line is an object, and both lines together would be part of an array. Names in the name and value pairs include firstname and lastname, while the value pairs would be the actual appearing names, like John, Jane and Doe.
JSON objects
JSON objects are unordered sets of name and value pairs. Objects are written inside of curly braces, like these { }. Everything inside the curly braces is part of the object. Objects can contain multiple name and value pairs. Each name is followed by a colon, and name value pairs are separated by a comma.
Objects can be accessed when needed and modified, deleted or looped.
JSON arrays
JSON arrays are an ordered list of values. Arrays are used to store objects, strings, number notation and Boolean notation. An array can be made up of multiple data types.
Arrays in JSON are surrounded by square brackets, like these [ ]. Each value in the array is separated by a comma. Users can access array values and update, delete or loop them. An array can be stored inside another JSON array; that's called a multidimensional array.
JSON conversions between text and object
There are two methods for converting between text and objects: parse() and stringify(). These methods might be used to read data from a web server when a developer has a JSON string and wants to convert it to an object. They can also be used when a user has a JavaScript object to send across a network that must be first converted to JSON.
Parse()
This method accepts a JSON string as a parameter and automatically returns a JavaScript object. To use parse(), create a JavaScript string that contains JSON syntax, and then use the function JSON.parse() to convert the string to a JavaScript object.
Stringify()
This method accepts an object as a parameter and automatically returns a JSON string. To use stringify(),create a JavaScript object, and then convert it using the stringify() function. After this, save the new value in a new variable.
JSON vs. HTML vs. XML: What are the differences?
Hypertext Markup Language is a text-based approach to describing how content contained within an HTML file is structured. This markup tells a web browser how to display text, images and other forms of multimedia on a web application.
Likewise, XML is another markup language. It's used to create formats for data used in encoding information for documentation, database records, transactions and other functions.
The main alternative to XML is JSON. Like XML, JSON is language-independent and can be combined with C++, Java, Python and other languages. Unlike XML, JSON is simply a way to represent data structures, as opposed to a full markup language.
XML is more difficult to manage than JSON format. Converting XML into a JavaScript object takes up to tens or hundreds more lines of code compared to JSON, and it requires an XML parser. XML documents are also more difficult to read when compared to JSON.
HTML is relatively simple compared to JSON. It does have more limitations, and JSON is more flexible. JSON also enables more complex data structures to be used in it than HTML. For example, HTML can't store values inside variables.
Learn more about JSON database capabilities and how they are used by different data platform vendors.