JavaScript Object Notation (JSON)

From Coder Merlin
Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder

What is JSON?[edit]

JSON, or JavaScript Object Notation, is a commonly used format that is used to transfer data. JSON is based on JavaScript, which means much of its syntax is familiar to programmers of other C languages, such as C, C++, Java, and many others. JSON is easy for machines to parse and generate, and easy for humans to read.

JSON Syntax[edit]

This following example shows possible syntax for describing a person through JSON.

{
  "firstName": "John",
  "lastName": "Smith",
  "isAlive": true,
  "age": 27,
  "address": {
    "streetAddress": "21 2nd Street",
    "city": "New York",
    "state": "NY",
    "postalCode": "10021-3100"
  },
  "phoneNumbers": [
    {
      "type": "home",
      "number": "212 555-1234"
    },
    {
      "type": "office",
      "number": "646 555-4567"
    }
  ],
  "children": [],
  "spouse": null
}

This code is derived from Wikipedia.

JSON Data Types[edit]

JSON supports 6 data types: String, Number, Boolean, Null, Object, and Array. String: Identified in double quotes “” but also supports various special characters such as \ (backslash), / (forward slash), b (backspace, n(newline), r(carriage return), t(horizontal tab) etc.

  • Let name = “david ben-yaakov”

Number: Represented in base 10. Octal and hexadecimal are not supported.

  • Let age = 20

Boolean: A value of true or false

  • Let teacher = true

Null: A nullable value, undefined.

Object: Rests in { } and is a set of names/values. Keys must be string and unique for each key. Names are separated by commas.

  • { key : value, …}

Array: Rests in [ ]. Arrays contains a collection of values separated by by commas.

  • [ value , ...]

JSON Uses[edit]

JSON’s format is primarily used for serializing and transferring structured data over network connections, more commonly used to transmit data between a server and web applications.

JSON Advantages[edit]

JSON has many advantages that make it the standard format for transmitting data in web applications. According to its developers and users, the format is easy to read/write for both humans and machines. Additionally, it is language independent which allows for it to be used to communicate and exchange data between different programming languages. Furthermore, JSON’s format design involves simple syntax that makes it lightweight and easy to run.

JSON vs Alternatives[edit]

On the other hand, there are a few key disadvantages to using JSON formatting that make many competitors a viable option in certain situations. One of JSON's main competitors is XML, a popular formatting method that includes standards such as HTML and HTML5. XML has more standards and has remained a consistent standard for years. By comparison, JSON doesn’t have a strictly defined standard leading to many different versions, some of which have become outdated and obsolete.

References[edit]