What is JSON?

·

2 min read

JavaScript Object Notation is simply a data representation format. It's very similar to XML and it's used for almost every API, widely across the internet. It's used because it's extremely lightweight to send back and forth due to the small file size, it's easy to read compared to something like XML (since it's much cleaner and there's not as many opening and closing tags).

Used for API's

You're going to use JSON all the time whether it's creating an API consuming an API or creating config files for you to use (VS Code, Gulp, ESLint, Babel).

JSON types:

As we know if JSON is a data representation format so we need to be able to represent a certain data types within it and JSON natively supports:

  • strings
  • numbers
  • booleans
  • null
  • arrays
  • objects

An object is the most complex but most used type within JSON and it allows you to represent values that are key value pairs so you give it a key and then a value.

Most of the time when you're working with JSON you'll have either an array or an object as your top level of your file and then inside of that array or object you'll have other values so it may even have other objects have other arrays.

Let's take an example of a user object that we want to put at the top level of our JSON file. To create an object we need to use opening and closing curly braces and then inside of that we'll put all of the key value pairs that make up our object. The key must be surrounded by double quotes followed by a colon. If we have multiple key value pairs we need commas separating every single one of our key value pairs similarly to how we would create an array in a normal programming language.

{
    "userId": 1,
    "id": 1,
    "title": "delectus aut autem",
    "completed": false
}

JSON has become especially popular with API code and web service developers because it speeds up data exchange and getting web service results. It is a text-based format that is lightweight and easy to parse and requires no additional coding to perform such parsing. For web services, JSON is an excellent choice because large amounts of data must be returned and displayed.