JSON: Difference between revisions

From Computer Science Wiki
No edit summary
No edit summary
Line 2: Line 2:


JSON, it is an open-standard file format that uses human-readable text to transmit data objects consisting of attribute–value pairs and array data types.
JSON, it is an open-standard file format that uses human-readable text to transmit data objects consisting of attribute–value pairs and array data types.
<syntaxhighlight>
<syntaxhighlight lang="JavaScript">
{
{
   "firstName": "Orest",
   "firstName": "Orest",

Revision as of 14:07, 29 June 2019

Web Science[1]

JSON, it is an open-standard file format that uses human-readable text to transmit data objects consisting of attribute–value pairs and array data types.

{
  "firstName": "Orest",
  "lastName": "Smith",
  "isAlive": true
}

# In the example above, firstname is an attribute, and the value is "Orest".

JSON in itself stands for JavaScript Object Notation, which basically means that it is used for Notating Objects, duh. JSON is a JS derivative, it uses very similar if not the exact same syntax as JS (example 1). What JSON is used for, and facilitates on a remarkable level is for serializing and transmitting structured data over a network connection. This is most commonly done to transmit data between a computer and a web application. It facilitates reading the code for the person writing it, but still lets the computer do its work, because it is a more ergonomic version of XML.

JSON still uses basic data types that most programming languages and formats use (example 2a-f). To show how JSON sets variables with the Basic Data Types, I have made Example 3. To walk you through it all in line 2 and on of the code, the String "Orest" to the variable "firstName", set the Boolean "true" to "isAlive", sets "age" to number "27", makes the array for address, which includes the more Strings, adds object "children", and sets "spouse" to null - basically 0.

To use JSON in a JS file, it just requires a quick link to another file (example 4). Any browser supporting ECMAScript fifth edition at minimum is able to parse JSON. Parsing JSON, using the JSON.parse(input) DOM API, however, is not intrinsically safe and should not be performed without ideally both safeguards or relative certainty of the provenance of the JSON data to be parsed. In cases where CORS is not possible, a common workaround minimal safeguard includes the use of JSONP, or JSON with padding, which wraps the JSON data in an additional set of curly braces which renders the output of the parsed JSON as an object and not itself, the inputted JSON, as simultaneously valid and potentially dangerous JavaScript script. (sorry if that's complicated, it is meant to be like that.)

Example 1 - String Value Application
JS: { name : 'Orest' }
JSON: { "name" : "Orest" }

Example 2 - Data Types
a. Number
b. String
c. Boolean
d. Array
e. Object
f. Null

Example 3 - Short Biography of a Person in short facts
{
  "firstName": "Orest",
  "lastName": "Smith",
  "isAlive": true,
  "age": 27,
  "address": {
    "streetAddress": "21 2nd Street",
    "city": "New York",
    "state": "NY",
    "postalCode": "10021-3100"
  },
  "phoneNumbers": [
    {
      "type": "home",
      "number": "123 654-7809"
    },
    {
      "type": "office",
      "number": "098 765-4321"
    },
    {
      "type": "mobile",
      "number": "123 456-7890"
    }
  ],
  "children": [],
  "spouse": null
}

Example 4 - Parsing JSON in JS
var p = JSON.parse(json_string);

Written and Edited by Orest

Standards[edit]

  • This topic doesn't cover a specific standard, but it is something you should you know.

References[edit]