JSON

JSON (JavaScript Object Notation) is a lightweight data-interchange format that is easy for humans to read and write, and for machines to parse and generate.

What does JSON do?

JSON’s human-readable text transmits data objects consisting of attribute-value pairs and array data types. It was derived from JavaScript, but its language is independent. As a result, JSON makes Web services faster and more scalable by reducing request latency.

While JSON is based on the object notation of the JavaScript language, it does not require JavaScript to read or write because it is a text format that is language-independent. JSON’s notation contains these basic elements:

  • Objects: Objects begin and end with curly braces ({}).
  • Object Members: Members consist of strings and values, separated by colon (:). Members are separated by commas.
  • Arrays: Arrays begin and end with braces and contain values. Values are separated by commas.
  • Values: A value can be a string, a number, an object, an array, or the literals true, false or null.
  • Strings: Strings are surrounded by double quotes and contain Unicode characters or common backslash escapes.

While XML uses tags, JSON uses key-value pairs represented as “key”: “value”. Note that there must be a double quote before key and after value. It looks like: {“name” : “value”}. The curly braces denote an object, and name/value pairs are denoted by a name followed by a colon followed by a value, enclosed in quotes. So, in JSON {“name”: “Joe”}, Joe is a string, and a name is an object with one member called name.  

An array is represented as a square bracket [ ], where commas separate items within brackets. So, in JSON [“1”, “2”], 1 and 2 are numbers, and each number is separated from another number by a comma.


Go deeper into developing with JSON

Developer.com


How is JSON used?

JSON has evolved as an efficient data-interchange format with many use cases. It’s used to transfer data on Web Pages and NoSQL databases. Here are some of JSON’s most common use cases: 

Data transfer between systems

JSON is a useful format for transferring data between different applications and systems. It allows developers to serialize and deserialize data quickly and efficiently while preserving its original structure. This makes it very easy to share information between multiple parties who might have different coding languages and programming backgrounds.

Configuring data for applications

JSON can be used to configure data for applications. For example, suppose a developer has a database and wants their application to read from it and write back changes. In that case, they may do so by defining what type of data their application expects through configuration files written in JSON. 

Generating a JSON object from user-generated data 

Developers often want to save user input into their database when users fill out a website form to access it later as needed. They could develop custom code to handle each form field separately; however, there is a better option: they may generate JSON objects directly from form fields.

Storing metadata about entities in databases 

Metadata is simply data about data. For example, a database table can contain various information, including columns describing what type of entity each row represents, how many records there are in total, how many records have been added or deleted since last time, etc.


How is JSON used in Microsoft software development?

Learn more at CodeGuru


Why was JSON created?

JSON was created as a lightweight format for exchanging data, particularly across applications and networks. As a result, it’s a fairly standard data transfer protocol that works well with most programming languages. It can be found in everything from APIs to webpages to video games. That’s because JSON is easy to read and write, making it ideal for both machines and humans alike. 

This data can be parsed by a JavaScript engine, making it ideal for transmitting hierarchical data structures or binary data over a network connection. JSON may also be used in situations where XML would be used, such as in RESTful APIs or JavaScript libraries. 

The official MIME media type for JSON is application/json. The filename extension is .json. Although JSON and JavaScript are loosely related, they are not identical. Data formatted according to JSON conventions can be manipulated directly by code written in any programming language that supports both JSON encoding and manipulation of basic data types. By contrast, arbitrary JavaScript code cannot manipulate JSON-formatted text directly; it must be converted into an intermediate representation compatible with whatever software environment is hosting the code. 

What did JSON replace? And is it better?

JSON was developed as an alternative to XML (Extensible Markup Language) for representing data in applications. It’s a lightweight format that’s easy to use and read and quickly becoming popular across web and mobile applications. JSON has several advantages over XML. Because the language adheres to JavaScript’s syntax, it’s easy for programmers to use both interchangeably in code, and it’s much easier to create a parser for JSON than XML; because of that, many developers are already familiar with how to read and write JSON documents. 

Most importantly, JSON’s files are smaller than XML and faster transmitting between servers. JSON data can be loaded quickly because its parsers are simpler, requiring less processing time and memory overhead.  XML is slower because it is designed for more than simply data interchange. Due to its simplicity, speed, and versatility, JSON has become popular in today’s development landscape.

Advantages

The major advantage of working with JSON compared to XML is that it is shorter and easier to read. The human-readability aspect makes it easier for humans to understand their data structure when implemented as an object. This can save time if a developer debugs or tests their application or website. 

  • Server parsing: JSON’s easy to parse on both the client and server sides. It’s very lightweight on parsing, which means less memory usage on the developer’s server-side. 
  • JSON is faster: Since JSON is lightweight, it loads quickly. Developers don’t have to wait long before seeing anything on the screen while parsing large data. JSON is faster than XML because it is explicitly designed for data interchange. 
  • Schema support: Unlike XML, JSON doesn’t require any schema definition upfront. This means that devs don’t need to worry about defining a schema first before sending any data over HTTP requests. 
  • Language independent: Being language-independent means that developers can use JSON across different programming languages. For example, C# uses the Newtonsoft.Json package to handle JSON objects, whereas PHP has built-in support for handling JSON objects using the json_encode() function. 
  • Data sharing tool: Being lightweight and language-independent make it perfect for sharing data between different devices/applications over the internet without worrying about compatibility issues across different platforms and programming languages. 
  • Interoperable: JSON works well with all modern web browsers and mobile applications. So users can share data seamlessly between desktop and mobile apps by using JSON format.

Shortcomings

While JSON has many advantages, it also has some disadvantages. 

  • Cannot be used to store binary data: This means that if a developer wants to store a file in JSON format, they’ll need to convert it into a text-based format first (like Base64). 
  • Doesn’t support comments: It’s possible to create comments, but parsers will ignore those comments because they aren’t valid JSON syntax characters.

Examples of JSON in use

JSON’s code can be written in a variety of ways. For example, it can be written as a string or object. In either case, it must begin with { and end with }. The following examples show how to create a JSON string and a JSON object. The first example shows how to create a simple string with two key-value pairs. 

The second example shows how to add multiple key-value pairs within one set of curly braces. This is known as an object. Note that a comma must separate each key-value pair. Additionally, each key must be followed by : (colon) and its value. Finally, note that all values are enclosed in double-quotes.

{“name” : “John Smith”}

Let’s define an employee's object: an array of 2 employee records (objects).

{
"employees":[
    {"firstName":"Rose", "lastName":"Mary"},
    {"firstName":"Cliton", "lastName":"Ford"},
]
}

The following syntax rules apply:

  • Data is in name/value pairs (for the first employee object: “FirstName” is the name and “Rose” is the value)
  • Data is separated by commas
  • Curly braces represent the JSON objects
  • Square brackets hold arrays

What is expected to replace JSON?

Although JSON has proven to be a popular data format and is unlikely to go away anytime soon, researchers and developers at MongoDB have found a way of further improving upon it. They call their creation BSON, short for binary JSON. It offers many of the same advantages as traditional JSON but with less overhead than its text-based counterpart. Other top JSON alternatives include:

YAML

YAML is a human-readable data serialization language. The acronym originally stood for YAML Ain’t Markup Language, but that initialism has since been dropped in favor of a backronym meaning YAML: Yet Another Markup Language. Its design goals include portability, ease of reading and writing, and interpretability by humans and computers. YAML can be used as a general-purpose data exchange format or as a way to store configuration files. 

Avro

Avro is a row-oriented remote procedure call and data serialization framework that runs on Apache Hadoop. It supports schema evolution and dynamic typing, which means developers can change their data structure without breaking clients that depend on it. This makes it easy to evolve schemas as their needs change over time. In addition, the schema is stored in JSON files, making it human-readable, editable, and portable across programming languages. 

MongoDB

MongoDB is a document-oriented database platform that uses JavaScript as its query language. MongoDB uses JSON-like documents with optional schemas and stores them in BSON, a binary representation of JSON. It supports server-side programming in multiple languages, including C++, Java, Perl, PHP, Python, and Ruby (JavaScript), and client-side programming in JavaScript (Node.js). 

Protobufs

Protocol buffers (or Protobufs) is an open-source, Google’s data interchange format. It allows the definition of structured data in a language-neutral form and efficient encoding and decoding of such definitions in many programming languages. Protobuf code can be compiled into a library or used directly in C++, Java, Python, Objective-C, Go, and C# code.

OData

OData (Open Data Protocol) is a data access protocol that provides uniform data access to various types of data services. It includes both Web-based and service-oriented architectures capabilities. Because OData uses standard HTTP requests and responses, it is easy for developers to integrate with their existing systems, regardless of what platform or programming language they use.


This article was reviewed and updated in April 2022 by Aminu Abdullahi.

Vangie Beal
Vangie Beal
Vangie Beal is a freelance business and technology writer covering Internet technologies and online business since the late '90s.

Related Articles

Firebase

Firebase is development software for building mobile and web applications. When Google acquired it in 2014, the tech giant made it their flagship platform...

AutoIt Scripting Language

AutoIt is a popular and easy-to-learn scripting language used by developers since 1999...

HighLevel CRM

HighLevel is a sales and marketing customer relationship management (CRM) solution designed by...

Unified Endpoint Management (UEM)

As enterprise networks become increasingly distributed with growing numbers of remote workers, unified...