Difference between revisions of "JavaScript Object Notation (JSON)"

From Coder Merlin
Line 33: Line 33:


== JSON Data Types ==
== JSON Data Types ==
*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.
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”
*Let name = “david ben-yaakov”


*Number: Represented in base 10. Octal and hexadecimal are not supported.
Number: Represented in base 10. Octal and hexadecimal are not supported.
Let age = 20
*Let age = 20


*Boolean: A value of true or false
Boolean: A value of true or false
Let teacher = true
*Let teacher = true


*Null: A nullable value, undefined.
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 ,
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, …}
*{ key : value, …}


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


== JSON Uses? ==
== JSON Uses? ==

Revision as of 10:35, 8 September 2021

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]

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]