Understanding JSON

Understanding JSON

A practical guide to truly understanding JSON, it's relationship and implementations with various programming languages - Dev P Academy

Welcome!

Please note: From on the 9th of September every new article being published will have an assessment test embedded, related to the course article being taken, don't miss out! Here's why. The aim of this is to help you slowly build your learning track records while you simply browse through for knowledge on our learning platform, Dev P Academy.

Introduction

For many who knows the acronym meaning of JSON, would conclude that JSON is can only be used in JavaScript(JS).

However, this is not the case. JavaScript Object Notation (JSON) is a data format that was built to be easily readable for both humans and computers. It has become the most popular way for applications to exchange data, and its most commonly encountered when working with APIs, but you’ll also find JSON files stored on servers, used in data exports, and many other places.

The minimal syntax and simple parsing of JSON increases the speed of communication in multiple ways. It’s often less data to download compared to other formats, and it can be interpreted in most modern languages without external libraries. For these reasons, along with being self-describing and easy-to-read, nearly 20 years after it was first created, JSON remains the data format choice for most developers.

JSON rose to popularity alongside browser programming languages that required compact and convenient serialization of data. It was originally intended to be a lightweight alternative to XML but has largely replaced it for easy parsing on the web. This guide provides historical context, useful benefits, and practical applications to better understand this important format.

Prerequisite

In taken this course, you acknowledge that you've had a good grasp of the fundamentals of programming and programming languages.

What is JSON?

JSON is a data interchange format that is easy to parse and generate. JSON is an extension of the syntax used to describe object data in JavaScript. Yet, it’s not restricted to use with JavaScript. It has a text format that uses object and array structures for the portable representation of data. All modern programming languages support these data structures, making JSON completely language independent.

A little JSON History

In the early 2000s Douglas Crockford created JSON to be minimal, portable, and textual. A subset of JavaScript (hence, the name), JSON came into popularity around the same time as the web browser scripting language. It was used widely at Yahoo!, where Crockford later worked as an architect. By the early 2010s, JSON was the popular choice for new public APIs.

It was standardized in 2013, as ECMA-404, and published in 2017 as RFC8259, the Internet Engineering Task Force (IETF) standard for the Internet. The RFC and ECMA standards remain consistent. The official Internet media type (also known as a Multipurpose Internet Mail Extensions or MIME type) for JSON is application/json, and JSON file names use the extension .json.

JSON grew out of a need for stateless, real-time server-to-browser communication protocol sans browser plugins like Flash or Java applets, the dominant methods used in the early 2000s. It aimed to be a lightweight alternative to XML to allow for mobile processing scenarios and easy parsing of JavaScript on the web.

JSON is commonly associated with REST services, especially for APIs on the web. Although the REST architecture for APIs allows for any format, JSON provides a more flexible message format that increases the speed of communication. It is useful when developing web or mobile applications where fast, compact, and convenient serialization of data is required.

JSON Structure, Syntax, and Usage

JSON’s simplicity is part of its appeal. It’s easy to write, easy to read, and easy to translate between the data structures used by most languages. Let’s look at what makes up a JSON object, the data types that JSON supports, and other specifics with the syntax of this popular data format.

Chances are you’ve seen JSON as you’ve looked through data or API documentation. The criteria for valid JSON is rather elementary, though it can be used to describe complex data. The structure of a JSON object is as follows:

  • Curly braces {} hold objects
  • The data are in key, value pairs
  • Square brackets [] hold arrays
  • Each data element is enclosed with quotes if it‘s a character, or without quotes if it is a numeric value
  • Commas are used to separate pieces of data

Here’s a basic example:

{ "name":"Katherine Johnson" }

The key is “name” and the value is “Katherine Johnson” in the above example. However, JSON can hold more than one key:value pair. This second example adds an “age” key, which includes a number and a second string value, assigned to the “city” key:

{ "name":"Katherine Johnson", "age":101, "city":"Newport News" }

It’s common to encounter nested JSON structures, like this:

{"inventors":[
    { "name":"Katherine Johnson", "age":101, "city":"Newport News" },
    { "name":"Dorothy Vaughan", "age":98, "city":"Hampton" },
    { "name":"Henry Ford", "age":83, "city":"Detroit" }
]}

In this final example, you see a primary object with a single key (“inventors”) that has an array as its value. Within that array, each item is itself an object, similar to the earlier simple example. Objects and arrays are values that can hold other values, so there’s an unlimited nesting that could happen with JSON data. That allows JSON to describe most data types, from tabular to even more complex.

What JSON is not!

  • It is a common mistake to call a JSON object literal "a JSON object".
  • JSON is not an object.
  • It simply follows JavaScript object format in storing it's data content.
  • JSON is a string format.

JSON Data types

This refers to the acceptable data types in which a value for a key in JSON can be stored in. The accepted data types for values in JSON are:

  • string – Literal text that’s enclosed in quotes.
  • number – Positive or negative integers or floating point numbers.
  • object – A key, value pair enclosed in curly braces
  • array – A collection of one or more JSON objects.
  • boolean – A value of either true or false with no quotes.
  • null – Indicates the absence of data for a key value pair, represented as “null” with no quotes.

Here’s an example of a JSON object that includes all of these data types:

{ 
    "name":"Katherine Johnson", 
    "age":101,
    "orbital_mechanics": ["trajectories","launch windows","emergency return paths"], 
    "mathmatician": true, 
    "last_location": null 
}

Characteristics of JSON literals

  • JSON literals are surrounded by curly braces {}.
  • JSON literals contains key/value pairs.
  • Keys and values are separated by a colon.
  • Keys must be strings, and values must be a valid JSON data type.

Rules in JSON Syntax

We’ve already discussed the structure of JSON, which provides the basics of the syntax. In this section, we’ll suggest some best practices to avoid common JSON errors:

  1. Always enclose the key, value pair within double quotes, like this:
    { "name": "Katherine Johnson" }
    
    Most JSON parsers don’t like to parse JSON objects with single quotes.
  2. Never use hyphens in your key fields. Use underscores ( _ ), all lower case, or camel case. For example:
    { "first_name":"Katherine", "last_name":"Johnson" }
    
  3. Use a JSON linter to confirm valid JSON. Install a command line linter or use an online tool like JSONLint. For example, if you copy this next example into a JSON linter,
    { "first_name":"Katherine", "last_name":'Johnson' }
    
    you should get a parse error for the pesky single quotes around the value for last_name.

Creating a JSON file content

Before we learn to open the JSON files, we need to create them. To create a sample JSON file follow the below basic steps.

  • Open the text editor on your computer.
  • Create a new file and save it.
  • Users need to save files with the .json extension.
  • Copy the below sample JSON code and paste into a file and save it again.

Storing data in JSON

Data in JSON are stored either in Object structure format or Array structure format.

Reading Data in JSON

JSON naturally is not affiliated and restricted to usage in JavaScript, simply that it takes the JavaScript object syntax's format in noting in data to be stored into it's file as it's content.

Glossary Word associated with JSON

  • Serialization:
  • json.parse(): converts a JSON content into an object.
  • json.stringify()

JSON can be used and is used by any programming language following the standard syntax in the programming language of reading and writing content in JSON.


JSON is a format of storing data. It's not a different language in programming. It's simply a format.

References

nylas.com/blog/the-complete-guide-to-workin..

youtube.com/playlist?list=PLEzQf147-uEoNCeD..