Discussing the Fundamentals of JSON

This article outlines the definition and essential purpose of JSON (JavaScript Object Notation). To set up the definition of JSON, a few key terms will be clarified.

In software development, a coding standard is used to give a uniform appearance, and lay out a specific set of use guidelines and best practices. There are a variety of these standards in use today: JSON is one.

A key-value pair (KVP) is a set of two linked data items. The "key" is a unique identifier for an item of data. The "value" will be either the data that is identified, or a pointer to the location of that data: both are a fundamental data representation. A key and a value create the pair.

The JSON standard is a data format used to organize data via key-value pairing, a format suited machine computing. An article from Daily Coding Info ranks JSON as the most used method of communication for web APIs, outpacing XML, CSV, and HTML. Daily Coding Info has presented a hypothetical use case for a JSON API in this article. The article presents a quick explanation of the format is included with the code:

"A "key-value" pairing in JSON is a format which consists of a left hand side, a colon, and a right hand side. The side left of the colon is the "key" and the side right of the colon is the "value". The left hand side, or the "key", is used to describe something about the right hand side, for example the "key-value" pairing...The braces in JSON denote an "object", an "object" is a collection of data inside JSON, starting with an open brace { and ending with a closing brace }."

A fundamental function of the braces is to serve as an unchangeable container - the form of the container may change, but the function remains consistent in each use.

JSON has 6 specific types of data. Four of the types are "simple" (Strings, Numbers, Boolean, and Null), two types are "complex" (Objects, and Arrays). Here are quick definitions of these types:

string data - a sequence of characters surrounded by quotes, useful when compiling data legible to humans. Examples:

"color" : "Purple"
"virus" : "active"
"genus" : "rosa"

number data - quoted numbers represented in base 10 (the decimal system), with exponents represented with an "E." Examples:

{
  "number_1" : 350,
  "number_2" : -350,
  "number_3" : 350.05,
   "number_4" : 3.0E+5
}

boolean - values which are "true" or "false." Example:

{
  "purple" : true
}

null - used to denote "nothing" or "void." Example:

{
  "purple" : true,
  "green" : ,   //empty
  "id" : 350
}

object - name/value pairs used in lists of "things," where the list is an unordered set of name/value pairs, in which the objects are (1) placed between {} (curly braces), (2) the objects contain zero or more name/value pairs, and (3) multiple name/value pairs are separated by a , (comma). Example:

{
  "purple" : true,
  "genus" : "rosa",
  "id" : 350
}

array - name/value pairs used in lists of "things," where the list is an ordered set of name/value pairs, in which the objects (1) begin with [ (left bracket) and ends with ] (right bracket) and (2) values are separated by , (comma). Example:

{
   "ids" : ["1","2","3"]
}

//or

{
  "ids" : [
    {"id" : 1},
    {"id" : 2},
    {"id" : 3}
  ]
}

These basic tenets make up the bulk of what is needed to understand JSON. Per the article from Daily Coding Info, "the concept of a "key-value" store and that "values" can have different "types" that's 90% of JSON." The final keystone of these tenets is understanding that JSON doesn't have a formal structure, meaning that the key-values can be ordered to individual preferences. Examples:

A JSON object can be added inside of another JSON object

{
 "rose_garden_name": "turnip the volume",
  "rose_garden_details": {
    "rose_color": "purple",
    "rose_count": 50,
    "rose_bushes": 350,
    "do_roses_have_thorns": true,
    "flowering_rose_bush_ids": [1, 2, 3],
    "dormant_rose_bushes": null,
    "by_another_name": {}
  }
}

JSON objects can be added inside of JSON arrays

{
  "rose_garden": [
    "turnip_the_volume",
    "rose_daisy",
    "the blooming rose",
      {
        "another_formal_garden_name": "daisy_shrub"
      }
    ]
}

JSON arrays can be added inside of JSON arrays

{
  "rose_garden": [
    "turnip_the_volume",
    "rose_daisy",
    "the blooming rose",
      [ "the_hillside_cottage_rose_garden", "coastal_farming_collective", "modern_mulching_regulations" ]
      ]
}

The ways to structure data in JSON are limitless, as long as they adhere to the use of a key and a value which is a valid JSON type of: Number, String, Boolean, Null, JSON object, Array.

Original Article

JSON The Language of the Internet

 

Comments