JSON v2: Difference between revisions

From John's wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 7: Line 7:
  var data = {
  var data = {
   
   
  // a JSON v2+ document is indicated by a sole 'json' property in the root data item
   "json": {
   "json": {
   
   
     // 'version' is only a 'major version', this should rarely change.
     // 'version' is only a 'major version' for the JSON format, this should rarely change
     "version": 2,
     "version": 2,
   
   
Line 24: Line 25:
     },
     },
   
   
    // note that the 'data' array can't indicate its namespace so is implicitly in the 'default' namespace
     "data": [
     "data": [
       {
       {
        // these properties don't have a namespace alias so are in the 'default' namespace
         "first_name": "John",
         "first_name": "John",
         "last_name": "Elliot",
         "last_name": "Elliot",
        // a namespace is indicated with its alias followed by a semi-colon
         "ex:lucky_number": 37
         "ex:lucky_number": 37
       }
       }
     ],
     ],
Line 36: Line 43:
       // so we have an inline schema for 'jj5' but no schema for 'ex':
       // so we have an inline schema for 'jj5' but no schema for 'ex':
       "jj5": {
       "jj5": {
         "$schema": "https://schema.jj5.net/json/example/v2/schema.json",
         "$schema": "https://schema.jj5.net/json/example/v2/schema.json",
         "title": "List of people",
         "title": "List of people",
Line 57: Line 65:
           }
           }
         },
         },
        // I'm not sure how to indicate an "open information model" where any extra unknown properties are allowed...
         "required": [ "first_name", "last_name" ]
         "required": [ "first_name", "last_name" ]
       }
       }
     }
     }
   }
   }
  };
  };

Revision as of 13:34, 8 July 2017

These are some thoughts on an improved JSON data-format that includes support for namespaces and schema description.

We want to optionally include a JSON schema for a namespace. Schema could be either inline or a URL which would return the schema. For example below the 'jj5' schema is inline but the 'example' schema is by-ref.

The rules for namespaces are that you have to nominate an alias for each namespace that you use. The alias 'default' is special (a reserved word) and it is used to point to the alias for the namespace which is assumed when no namespace alias appears on data items. The 'version' and 'namespace' data is listed first, then the 'data' itself, then the 'schema' data if it's available. We put the schema last because it's the least interesting thing to read when a human is looking at the data.

var data = {

  // a JSON v2+ document is indicated by a sole 'json' property in the root data item
  "json": {

    // 'version' is only a 'major version' for the JSON format, this should rarely change
    "version": 2,

    // a map of namespace aliases and the namespace they refer to
    "namespace": {

      // the 'default' namespace alias is the alias implied if no alias is specified
      "default": "jj5",

      // the formal namespaces are a URL the details of which may vary as indicated in these examples
      "jj5": "https://schema.jj5.net/json/example/v2/schema.json",
      "ex": "https://v1.example.schema.json.example.com/schema.json"

    },

    // note that the 'data' array can't indicate its namespace so is implicitly in the 'default' namespace
    "data": [
      {

        // these properties don't have a namespace alias so are in the 'default' namespace
        "first_name": "John",
        "last_name": "Elliot",

        // a namespace is indicated with its alias followed by a semi-colon
        "ex:lucky_number": 37

      }
    ],

    "schema": {

      // so we have an inline schema for 'jj5' but no schema for 'ex':
      "jj5": {

        "$schema": "https://schema.jj5.net/json/example/v2/schema.json",
        "title": "List of people",
        "type": "array",
        "items": {
          "title": "Person",
          "type": "object",
          "properties": {
             "first_name": {
                "type": "string"
              },
              "last_name": {
                "type": "string"
              },
              "age": {
                "description": "Age in years",
                "type": "integer",
                "minimum": 0
              }
            }
          }
        },

        // I'm not sure how to indicate an "open information model" where any extra unknown properties are allowed...
        "required": [ "first_name", "last_name" ]

      }
    }
  }
};