avatarMax N

Summarize

The Power of JSON

A Beginner’s Guide to the Data Format That’s Changing How We Share Information Online

Photo by Pankaj Patel on Unsplash

Have you ever wondered how modern web apps manage to seamlessly display dynamic content from different sources? How your favorite shopping sites instantly populate with the latest products and prices without needing a page refresh?

The technology behind this is likely JSON — the web’s data format superpower that has revolutionized how information gets shared on the internet.

In this beginner’s guide, we’ll unpack exactly what JSON is and why it has become so popular in recent years. More importantly, we’ll see just how easy it is to get started with JSON and use it yourself across projects. Let’s dive in!

What is JSON and Why Does it Matter?

JSON stands for JavaScript Object Notation. First off — it is not a programming language but rather a common standard file and data transfer format that is incredibly easy for humans and machines alike to read and generate.

It represents structured data in simple key-value pairs and ordered lists. What makes JSON so vital is its portability — this lightweight text-based structure means that data in JSON format can seamlessly integrate with virtually any programming language.

It also stores information in an organized, easy-to-access manner.

The Rise of JSON

Though JSON was first formally specified in the early 2000s and has its roots in JavaScript, it has skyrocketed in popularity over the last decade thanks to the explosive growth of web APIs and cloud services.

JSON has essentially become the “lingua franca” of modern web development. Its browser-readable text format, combined with its JavaScript-friendly syntax, means that JSON-encoded data can move fluidly between client-side JavaScript web apps and web servers.

Developers favor sending JSON objects back and forth through APIs because it plugs directly into JavaScript on web pages. And because JSON is lightweight compared to other similar data formats like XML, it is far faster to parse and process.

Let’s Create Our First JSON

One of the best parts about getting started with JSON is seeing just how readable and intuitive its format is for humans. Let’s have a look at an example JSON object:

{
  "squadName": "Superhero Squad",
  "homeTown": "Metro City", 
  "formed": 2016,
  "secretBase": "Super tower",
  "active": true,
  "members": [
    {
      "name": "Molecule Man",
      "age": 29,
      "secretIdentity": "Dan Jukes",
      "powers": [
        "Radiation resistance",
        "Turning tiny",
        "Radiation blast"
      ]
    },
    {
      "name": "Madame Uppercut",
      "age": 39,
      "secretIdentity": "Jane Wilson",
      "powers": [
        "Million tonne punch",
        "Damage resistance",
        "Superhuman reflexes"
      ]
    }
  ]
}

We have a JSON object here representing a Superhero Squad, with different attributes like name, hometown, year formed etc. Nested inside we also have an array called “members” listing the members of this squad. This demonstrates JSON’s straightforward structure:

  • Uses key-value pairs to store data points — “homeTown”: “Metro City”
  • Keys must be strings, wrapped in double quotes — “formed”: 2016
  • Values can be strings, numbers, Booleans, arrays, or other JSON objects
  • Uses ordered lists for sequential data, denoted by [ ]

Lists allow us to bunch related attributes together. For example, all powers belonging to a member are together instead of separate properties.

This modular approach makes JSON extremely flexible. We can model complex hierarchies of data while still ensuring readability for both humans and machines.

Interacting with JSON Using JavaScript

One reason JSON is so popular among JavaScript developers is that it integrates directly with JavaScript syntax and standard APIs, making it a breeze to handle JSON data within JS web apps.

Let’s take our previous JSON and carry out some common operations on it using vanilla JavaScript:

const squad = {
  // JSON object from before
}

// Access data 
let hometown = squad.homeTown; 

// Loop through arrays
squad.members.forEach(member => {
  console.log(member.name); 
});

// Add new data
squad.leader = "Molecule Man"; 

// Stringify JSON 
let stringified = JSON.stringify(squad);

// Parse back 
let parsed = JSON.parse(stringified);

Here we can access values directly, loop through the members array, add new properties, stringify into a string and parse it back into JSON…all using native JavaScript without any special JSON libraries!

This tight integration makes JSON the ideal data format for client-side web apps using modern JavaScript frameworks like React and Vue.js.

It can power dynamic visualizations and charts that update continually as new JSON packets stream in.

Next Steps with JSON

We’ve explored the absolute basics, but there is much more that can be done with JSON thanks to its flexibility. Entire APIs are built around sending JSON data to web and mobile apps.

The portability of JSON means it can integrate just as smoothly with server-side languages like Java, Python, Ruby, PHP and more.

If you found this explainer helpful and you want to learn how professional developers are building full-stack web apps with JSON, check out my complete course on Modern JavaScript Web Development.

See how JSON enables us to architect scalable apps and supercharge how client and servers share and update data seamlessly.

Json
JavaScript
Object Oriented
Javascript Tips
Javascript Development
Recommended from ReadMedium