JS-151-API
  • JavaScript Library
  • 02-DOM-Manipulation
    • Overview
    • DOM Explained
    • Example Setup
    • Selecting Elements
    • Events
    • Complete Example
  • 03-API Fundamentals
    • 0-Getting Started
      • Welcome
    • 1-Intro-To-APIs
      • Intro
      • Client
      • Requests
      • JSON
      • API Endpoints
      • Server
      • Response
      • Statelessness
      • REST
    • 2-Asynchronous-Programming
      • Intro
      • Callbacks
      • Promises
      • Promises Continued
      • Chaining Promises
    • 3-Fetch
      • Fetch Intro
      • Star Wars
        • Star Wars API
        • Star Wars Setup
        • Star Wars JS
      • Random Photo
        • Unsplash
        • Unsplash Setup
        • Unsplash JS
    • 4-Apps
      • 01-New York Times
        • 00-App Intro
        • 01-HTML/CSS/API Key
        • 02-Variables
        • 03-Event Listeners
        • 04-fetchResults
          • 01-fetchResults()
          • 02-preventDefault()
          • 03-fetch() method
          • 04-Dates
        • 05-displayResults
          • 01-Logging JSON
          • 02-Working with JSON
          • 03-Link Display
          • 04-Results Navigation
          • 05-Results Update
          • 06-Keywords
          • 07-Images
        • 06-Pagination
          • 01-Pagination Intro
          • 02-Pagination Functions
        • 07-Next Steps
      • 02-YouTube
        • html
        • youtube.css
        • youtube.js
      • 03-GoogleMaps
        • Setup
        • HTML and CSS files
        • API Key
        • JS Setup
        • Adding Your Location
        • Listeners
        • Custom Options
Powered by GitBook
On this page
  • What is JSON?
  • Example
  • JSON Vs. JavaScript Objects
  • Minified JSON
  1. 03-API Fundamentals
  2. 1-Intro-To-APIs

JSON

What is JSON?

So now that we have our HTTP requests, how can we pass data between the browser and server? We use JSON! JSON is short for JavaScript Object Notation. JSON can convert any JavaScript object into readable text that can be sent to the server or vice versa. Because it can be created using almost any language, JSON objects are the most commonly used way to pass data through HTTP requests.

Example

Let's see an example using a superhero's profile in JSON.

{
    "name": "Superman",
    "age": 79,
    "secretIdentity": "Clark Kent",
    "powers": [
        "Super strength",
        "Heat vision",
        "Flight",
        "X-ray vision",
        "Super speed"
    ]
}

JSON Vs. JavaScript Objects

At first glance, you're might be thinking "Hey! This looks like a JavaScript object!". This is partially true, as the syntax is derived from JavaScript. However, there is a small but important difference involving the name part of a name/value pair.

Let's use our first item to demonstrate, first in JSON:

{
"name": "Superman"
}

If this were a JavaScript object, it could be written without the double quotes around name:

name: "Superman"

The reason this doesn't work for JSON is because JSON is a text-only-format, for consistency across multiple languages.

Minified JSON

JSON can be minified like CSS, JavaScript, and many other languages. Minified just means condensing information to make file sizes smaller, for more efficient loading over the web. Specifically, it is the process of removing all unnecessary characters from source code without changing its functionality.

Let's return to our superhero example to see exactly what is happening, first we'll see the JSON before it is minified.

{
    "name": "Superman",
    "age": 79,
    "secretIdentity": "Clark Kent",
    "powers": [
        "Super strength",
        "Heat vision",
        "Flight",
        "X-ray vision",
        "Super speed"
    ]
}

Minified JSON:

{"name":"Superman","age":79,"secretIdentity":"Clark Kent","powers":["Super strength","Heat vision","Flight","X-ray vision","Super speed"]}

Minifying removes all the extra white space in your code. No white space means a smaller file size and in turn faster/better performance. It may not seem like much, but those extra blank characters can make a huge difference in size when dealing with hundreds or even thousands of lines of code.

PreviousRequestsNextAPI Endpoints

Last updated 7 years ago