Wednesday 15 July 2020

Introduction to JSON

JSON (JavaScript Object Notation) is:
  • lightweight data-interchange format
  • open standard file format
  • easy for humans to read and write
  • easy for machines to parse and generate
  • uses human-readable text to store and transmit data objects



Arrays


Valid JSON: array of objects:

[
   {
"a": 1
   }, 
   {
"b": 2
   }
]

Valid JSON: array of arrays where each array has a single element:

[
[
  {
"a": {}
}
 ],
[
  {
"b": {}
}
 ]
]


How to represent an array of integers?


JSON for List of int

{
    "Id": "610",
    "Name": "15",
    "Description": "1.99",
    "ItemModList": [
                       0,
                       375,
                       668,
                       5,
                       6
                   ]
}



JSON (object) vs JSON string



JSON object is an instance of the data type (e.g. Object in JavaScript) which represents a JSON (structure) and which can be initialised with a JSON literal:

var jsonObject = { "prop": "val" };

JSON string is a string:

var jsonString = '{ "prop": "val" }';

JavaScript provides methods for converting JSON Object to string:

JSON.stringify to convert objects into JSON string.
JSON.parse to convert JSON string back into an object.


No comments: