TechAid24

Your Technical Consultant

The Ultimate JSON Cheat Sheet : Boost Your Productivity as an IT Professional

Posted on by Anonymous Guest 3 minutes read

JSON (JavaScript Object Notation) is a lightweight data interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. JSON is a text format that is completely language-independent but uses conventions that are familiar to programmers of the C family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language. Let’s start JSON Cheat Sheet.

Data Types
number
var myNum = 123.456;
Series of numbers; decimals ok; double­-pr­ecision floati­ng-­point format
string
var myString = “­abc­def­”;
Series of characters (letters, numbers, or symbols); double­-quoted UTF-8 with backslash escaping
boolean
var myBool = true;
true or false
array
var myArray = [ “­a”, “­b”, “­c”, “­d” ];
sequence of comma-­sep­arated values (any data type); enclosed in square brackets
object
var myObject = { “­id”: 7 };
unordered collection of comma-­sep­arated key/value pairs; enclosed in curly braces; properties (keys) are distinct strings
null
var myNull = null;
variable with null (empty) value
undefined
var myUnde­fined;
variable with no value assigne
Objects
var myObject = {
  "first": "John",
  "last": "Doe",
  "age": 39,
  "sex": "male",
  "salary": 70000,
  "registered": true
};
Access object properties
myObje­ct.sex
returns “­mal­e”
myObje­ct[­”­age­”]
returns 39
myObje­ct[0]
returns “­Joh­n”
myObje­ct.s­om­ething
returns undefined
myObje­ct[6]
returns undefined
Array of objects
var myArray = [
  {
    "first": "John",
    "last": "Doe",
    "age": 39,
    "sex": "male",
    "salary": 70000,
    "registered": true
  },
  {
    "first": "Jane",
    "last": "Smith",
    "age": 42,
    "sex": "female",
    "salary": 80000,
    "registered": true
  },
  {
    "first": "Amy",
    "last": "Burnquist",
    "age": 29,
    "sex": "female",
    "salary": 60000,
    "registered": false
  }
];
Access array elements
myArray[0]
returns { “­firs­t”: “­Joh­n”, “­las­t”: “­Doe­” … }
myArray[1]
returns { “­firs­t”: “­Jan­e”, “­las­t”: “­Smi­th” … }
myArra­y[1­].first
returns “­Jan­e”
myArra­y[1][2]
returns 42
myArra­y[2­].r­egi­stered
returns false
myArray[3]
returns undefined
myArra­y[3­].sex
error: “­cannot read proper­ty…”
Arrays
var myArray = [
  "John",
  "Doe",
  39,
  "M",
  70000,
  true
];
Access array elements
myArray[1]
returns “­Doe­”
myArray[5]
returns true
myArray[6]
returns undefined
Nested objects and arrays
var myObject = {
  "ref": {
    "first": 0,
    "last": 1,
    "age": 2,
    "sex": 3,
    "salary": 4,
    "registered": 5
  },
  "jdoe1": [
    "John",
    "Doe",
    39,
    "male",
    70000,
    true
  ],
  "jsmith1": [
    "Jane",
    "Smith",
    42,
    "female",
    80000,
    true
  ]
};
Access nested elements
myObje­ct.r­ef.first
returns 0
myObje­ct.j­doe1
returns [ “­Joh­n”, “­Doe­”, 39 … ]
myObje­ct[2]
returns [ “­Jan­e”, “­Smi­th”, 42 … ]
myObje­ct.j­sm­ith1[3]
returns “­fem­ale­”
myObje­ct[­1][5]
returns true
myObje­ct.j­do­e1[­myO­bje­ct.r­ef.last]
returns “­Doe­”
myObje­ct.j­sm­ith­1[m­yOb­jec­t.r­ef.age]
returns 42
JSON Cheat Sheet

Tags:

Anonymous Guest

An anonymous author is a writer who conceals their true identity when publishing content, often for privacy, security, or confidentiality reasons. This approach is often used in technical articles to focus on the information being presented rather than the author's personal identity.

Leave a Reply

Your email address will not be published. Required fields are marked *