Intro To JSON & JSON Schema

As a web developer, it’ll be hard to not come across JSON or JavaScript Object Notation, so understanding it is important as a webdev. It is human readable and easily parsed by a machine and often used as a common format to serialize and deserialize data in applications that communicate with each over on the web. It is an open standard and commonly used in RESTful (Representation State Transfer) web services.

JSON exists as only two data structures: objects and arrays. An object is a set of name-value pairs:

{ "name": "value" }

An array of a list of values:

[1, 2, 3, "four"]

JSON has seven value types:

// string
{ "type": "some-string" }

// number
{ "type": 2 }

// object
{ "type": { "obj": "object" } }

// array
{ "type": [1, 2] }

// true
{ "type": true }

// false
{ "type": false }

// null
{ "type": null }

There is a specification for JSON called JSON schmea. It describes a JSON format, can be used for automated testing and can be used to validate data. There are several validators in different languages to validate a JSON object given a JSON schema. An example looks like this:

{
   "$schema": "http://json-schema.org/draft-04/schema#",
   "title": "Product",
   "description": "A product from Acme's catalog",
   "type": "object",

   "properties": {

      "id": {
         "description": "The unique identifier for a product",
         "type": "integer"
      },

      "name": {
         "description": "Name of the product",
         "type": "string"
      },

      "price": {
         "type": "number",
         "minimum": 0,
         "exclusiveMinimum": true
      }
   },
   "required": ["id", "name", "price"]
}

Learn more about JSON Schema

Read more about JSON here

Instagram Post