JSON CheatSheet

Proficiently working with JSON data can be facilitated by utilizing a comprehensive JSON cheatsheet, which serves as an indispensable resource for developers. This tool provides a succinct overview of JSON syntax and structure, facilitating the swift creation, reading, updating, and deletion of JSON objects and arrays. By judiciously leveraging this valuable reference guide, developers can optimize their coding workflow, leading to greater efficiency and ultimately.


Table of Content




# Getting started JSON


What is JSON ?

JSON is a lightweight text-based open standard designed for human-readable data interchange.

JSON is a minimal, readable format for structuring data. It is used primarily to transmit data between a server and web application, as an alternative to XML. Squarespace uses JSON to store and organize site content created with the CMS.

  • JSON stands for JavaScript Object Notation
  • JSON is easy to read and write.
  • JSON is language agnostic data-interchange format
  • JSON filename extension is .json
  • JSON Internet Media type is application/json

JSON syntax involves key-value pairs, where keys are always strings, and values can be strings, numbers, objects, arrays, booleans or null. Data in JSON format is typically represented in two structures: an object, which is an unordered set of key-value pairs enclosed in curly braces ({}), or an array, which is an ordered list of values enclosed in square brackets ([]).

Here's an example of a simple JSON object:

  
  {
   "name": "John",
   "age": 30,
   "isMarried": true,
   "hobbies": ["reading", "traveling"]
  }
   
  

In this example, we have an object with four key-value pairs, including a string value for "name", a number value for "age", a boolean value for "isMarried", and an array of string values for "hobbies".

JSON is widely used in web development, and it has become a popular data interchange format due to its simplicity, flexibility, and ease of use.

Keys and Values

The two primary parts that makeup JSON are keys and values. Together they make a key/value pair.

  • Key: A key is always a string enclosed in quotation marks.
  • Value: A value can be a string, number, boolean expression, array, or object.
  • Key/Value Pair: A key value pair follows a specific syntax, with the key followed by a colon followed by the value. Key/value pairs are comma separated.

JSON Advantages

JSON is Faster: JSON syntax is very easy to use. We have to use only -> as a syntax which provides us an easy parsing of the data and faster execution of the data. Since its syntax is very small and light-weighted thats the reason that it executes the response in a faster way.

Schema Support: It has a wide range of supported browser compatibility with the operating systems so the applications made with the coding of JSON dont require much effort to make it all browser compatible. During development, the developer thinks for the different browsers but JSON provides that functionality.

Server Parsing: On the server-side parsing is the important part that developers want if the parsing will be fast on the server side then the only user can get the fast response of their response so in this case JSON server-side parsing is the strong point that indicates us to use the JSON on the server-side.

Tool for sharing data: JSON is the best tool for the sharing data of any size even audio, video etc. This is because JSON stores the data in the arrays so data transfer makes it easier. For this reason, JSON is a superior file format for web APIs and for web development.

JSON Disadvantages

First and foremost, in JSON has no error handling for JSON calls. If the dynamic script insertion works, you get called and will get the response perfectly.

If not inserted, nothing happens. It just fails silently. For example, you are not able to catch a 404 error from the server, Nor can you cancel or restart the request. You can, however, timeout after waiting a reasonable amount of time.

Another major drawback of JSON is that it can be quite dangerous if used with untrusted services or untrusted browsers, because a JSON service returns a JSON response wrapped in a function call, which will be executed by the browser if it will be used with untrusted browser it can be hacked, this makes the hosting Web Application Vulnerable to a variety of attacks.

If you are going to use JSON services, its very important to be aware of the threats which JSON have in that and also be aware of the things which can protect it. JSON only have limited supported tools which we can use during JSON development.

JSON Examples

{
      "name": "Jack",
      "age": 60,
      "height": 2.5,
      "gender": "M",
      "salary": 70000,
      "married": true,
      "address": "3431 Swaffer Rd Millington, Michigan(MI), 48746",
      "children": [
          {
              "name": "Billy",
              "age": 20,
              "gender": "M"
          },
          {
              "name": "Salla",
              "age": 16,
              "gender": "F"
          }
      ]
  }

Types

Type Description
Number Double precision floating-point
String Series of characters
Boolean true or false
Array Ordered sequence of values
Value String, Number, Boolean, null etc
Object Unordered collection of key/value pairs
null Null or Empty

String

\" Double quote
\\ Backslash
\/ Forward slash
\b Backspace
\f Form feed
\n Newline
\r Carriage return
\t Tab
\u Trailed by four hex digits
// Examples
  {
      "url": "https://google.com",
      "msg": "Hi,\n\"google.com\"",
      "intro": "This is google"
  }
  
  // Invalid String
  {
      "foo": 'hello word'
  }

Number

Type Description
Integer Digits 1-9, 0 and positive or negative
Fraction Fractions like 0.3, 3.9
Exponent Exponent like e, e+, e-, E, E+, E
// Examples
  {
    "positive" : 12,
    "negative" : -1,
    "fraction" : 10.25,
    "exponent" : 1.0E+2,
    "zero" : 0
  }
  
  // Invalid Number
  {
      "number": 0xxxF
  }

Objects

{
    "color": "Purple",
    "id": "210",
    "composition": {
      "R": 70,
      "G": 39,
      "B": 89
    },
    "empty_object": {}
  }

Array of objects

{
      "children": [
          {
              "name": "Jack",
              "age": 15
          },
          {
              "name": "Rose",
              "age": 12
          }
      ]
  }

Arrays

[1, 2, 3, 4, 5]

Object of arrays

{
      "attributes": [
          "a1",
          "a2"
      ],
      "methods": [
          "getter",
          "setter"
      ],
      "empty_array": []
  }

2D Array

{
      "your_seq": [
        [1, 2, 3],
        [4, 5, 6],
        [7, 8, 9, 0],
        [10, 11]
      ]
  }

Object of objects

{
    "Mark McGwire": {
      "hr": 65,
      "avg": 0.278
    },
    "Sammy Sosa": {
      "hr": 63,
      "avg": 0.288
    }
  }

Nested

{
      "Jessy": {
          "id": 1,
          "name": "Green",
          "salary": 100000,
          "hobby": [
              "sings",
              "football"
          ],
          "location": {
              "country": "B",
              "city": "B-B"
          }
      }
  }

# How to Access JSON in JavaScript


Access Object

myObject.name "Jason"
myObject["name"] "Jason"
myObject.age 39
myObject.other undefined
myObject[0] undefined
let myObject = {
    "name": "Jason",
    "last": "Doe",
    "age": 39,
    "gender": "M",
    "salary": 70000,
    "married": true
  };

Access Nested

myObject.ref.age 2
myObject["ref"]["age"] 2
myObject.jdoe ["Jason", "Doe", 39 ...]
myObject.jsmith[3] "F"
myObject[1] undefined
let myObject = {
      "ref": {
          "name": 0,
          "last": 1,
          "age": 2,
          "gender": 3,
          "salary": 4,
          "married": 5
      },
      "jdoe": [
          "Jason",
          "Doe",
          39,
          "M",
          70000,
          true
      ],
      "jsmith": [
          "Tom",
          "Smith",
          42,
          "F",
          80000,
          true
      ]
  };

Access Array of Objects

myArray[0] {"fist": "Jason", ...}
myArray[1].name "Tom"
myArray[1][2] 42
myArray[3] undefined
myArray[3].gender TypeError: Cannot read...
let myArray = [
      {
          "name": "Jason",
          "last": "Doe",
          "age": 39,
          "gender": "M",
          "salary": 70000,
          "married": true
      },
      {
          "name": "Tom",
          "last": "Smith",
          "age": 42,
          "gender": "F",
          "salary": 80000,
          "married": true
      },
      {
          "name": "Amy",
          "last": "Burnquist",
          "age": 29,
          "gender": "F",
          "salary": 60000,
          "married": false
      }
  ];

Access Array

myArray[1] "Doe"
myArray[5] true
myArray[6] undefined
let myArray = [
      "Jason",
      "Doe",
      39,
      "M",
      70000,
      true
  ];


Best Suggest