Skip to content

JSON - is a cup of tea

Published: at 6 min read

JSON and its introduction

JSON

It is a text-based format used for

JSON is ~

Usage -

It is often used for configuration files, data storage, and communication between a web server and a web client, among other applications.

Lets explore about JSON

JSON data is represented as key-value pairs

where keys are strings enclosed in double quotes, followed by a colon, and then the associated value.

The value can be a ~

and these comes under JSON Syntax.

Lets explore an example of JSON data:

{
  "name": "Malti Sharma",
  "age": 30,
  "isStudent": false,
  "gender": "female",
  "hobbies": ["reading", "gardening", "traveling", "poetry"]
}

In the above example, the JSON object has five key-value pairs:

  1. “name” with the string value “Malti Sharma"
  2. "age” with the number value 30
  3. ”isStudent” with the boolean value false
  4. ”gender” with the string value “female"
  5. "hobbies” with an array value containing four strings.

JSON and its Advantages

JSON Usage

  1. Transfers data between a server and various web applications.
  2. Performs asynchronous data calls without requiring a page refresh. This is widely used for asynchronous browser/server communication
  3. APIs and web services use JSON to transfer real-time sensitive data.
  4. Compiles user data for local storage.
  5. JSON is now being supported by almost all Programming languages.
  6. JSON supports for sending, reading, and receiving texts available in all the real-world business.
  7. JSON-RPC is a Remote Procedure call (RPC) protocol built on JSON, which allows system to send multiple notifications to the server.

JSON Vs XML

PropertiesJSONXML
AbbreviationJavascript Object NotationeXtensible Markup Language
Data RepresentationUses a lightweight, human-readable format that is based on key-value pairs. Data is represented using objects (curly braces {}) and arrays (square brackets []).Uses a markup language with a more verbose and hierarchical structure. Data is enclosed in tags, often resembling an HTML structure with opening and closing tags.
ReadabilityGenerally considered more readable by humans due to its simpler syntax and lack of excessive markup.Can be more verbose and harder to read due to the presence of tags and attributes.
ParsingEasier and faster to parse in JavaScript and many other programming languages because it maps directly to data structures like dictionaries and lists.Requires more complex parsing, often involving the use of XML parsers and libraries.
DataTypesSupports a limited set of data types, including strings, numbers, booleans, null, objects, and arrays.Allows for more complex data types and custom data structures through the use of schemas.
SizeGenerally produces smaller payloads compared to XML due to its concise syntax.Tends to produce larger data payloads because of the additional markup.
UsageCommonly used in web APIs, configuration files, and data interchange in JavaScript-based applications and RESTful web services.Historically used in web services (SOAP), document storage (e.g., RSS, XHTML), and data interchange in various domains, including finance and healthcare.
Schema ValidationSupports schema validation through technologies like JSON Schema, but it’s not as widely adopted as XML schemas.Has robust support for schema validation through technologies like Document Type Definitions (DTD) and XML Schema Definition (XSD).

JSON and Limitations

JSON Syntax and Rules

jsonSyntax

Rules ~

Invalid DataTypes - JSON

JSON values cannot use any of the following data types:

JSON is built on two important structures:

JSON Parse

// A JSON string representing an object
var jsonString = '{"name": "Johny Ryan", "age": 30, "city": "New York"}';

// Parse the JSON string into a JavaScript object
var jsonObject = JSON.parse(jsonString);

// Access values in the JavaScript object
console.log("Name:", jsonObject.name); // Output: Name: Johny Ryan
console.log("Age:", jsonObject.age); // Output: Age: 30
console.log("City:", jsonObject.city); // Output: City: New York

JSON parsing refers to the process of

Parsing JSON is a common task when working with data received from a web server, reading configuration files, or processing data in JSON format.

While receiving data from the web server, the data is always in the string format, we have to parse the data with JSON.parse() and the data becomes a JavaScript object.

jsonParse

JSON Stringify

// A JavaScript object
var person = {
  name: "Johny Ryan",
  age: 30,
  city: "New York",
};

// Convert the JavaScript object into a JSON-formatted string
var jsonString = JSON.stringify(person);

// Display the JSON string
console.log(jsonString);

Output :

{ "name": "Johny Ryan", "age": 30, "city": "New York" }

jsonStringify

JSON and its Family Libraries

Apart from the regular JSON, we have other versions of JSON in use as well.

  1. Google’s GSON - Java library from Google to convert Java Objects into JSON and vice versa. In addition, it paves room for simpler implementation by not requiring to annotate your classes.

  2. Oracle’s JSONP is Java API for JSON processing. This consumes/produces streaming JSON text.

  3. FasterXML’s Jackson - Can handle both JSON/non-JSON encodings. It is a set of data processing tools powered with streaming JSON parser and generator library.

JSON Security Concerns and Overcoming steps

How to overcome these threats while using JSON

Summary

JSON has become a popular format for data exchange on the web as well as in other applications because of its simplicity, human-readability, and compatibility with many programming languages. It is widely used in RESTful APIs, AJAX requests, and configuration files for various software applications.

Share :
Written by:Parita Dey

Interested in Writing Blogs, showcase yourself ?

If you're passionate about technology and have insights to share, we'd love to hear from you! Fill out the form below to express your interest in writing technical blogs for us.

If you notice any issues in this blog post or have suggestions, please contact the author directly or send an email to hi@asdevs.dev.