Skip to content

Instantly share code, notes, and snippets.

@50kudos
Last active March 13, 2022 06:02
Show Gist options
  • Save 50kudos/642430e0789b688e4723fa68bf2340a8 to your computer and use it in GitHub Desktop.
Save 50kudos/642430e0789b688e4723fa68bf2340a8 to your computer and use it in GitHub Desktop.
Json with metadata

json-thick is json whose node has metadata attached along with its value.

Purpose

Work with human interaction either directly with json file or as input/output format used by other user interfaces (e.g. GUI), with extra information on each value, suitable for guided processing or data persistence.

Example

Thick

{
  "c": "root comment",
  "v": {
    "a": {
      "v": {
        "i": { "v": 1, "c": "leaf node comment"},
        "j": { "v": 2 }
      },
      "c": "object node comment"
    },
    "b": {
      "v": [{"v": null}, {"v": null}],
      "c": "array node comment"
    },
    "c": {"v": "a"},
    "d": {"v": 1},
    "e": {"v": true},
    "f": {"v": null}
  }
}

Thin

{
  "a": {
    "i": 1,
    "j": 2
  },
  "b": [null, null],
  "c": "a",
  "d": 1,
  "e": true,
  "f": null
}
Size comparison (minified)

Thick : 228 bytes
Thick : 134 bytes without "c" (comments)
Thin : 68 bytes

JSON Schema

{
  "type": "object",
  "properties": {
    "v": { 
      "oneOf" [
        { "type": "object", "additionalProperties": { "$ref": "#" } },
        { "type": "array", "items": { "$ref": "#" } },
        { "type": "string" },
        { "type": "number" },
        { "type": "boolean" }
        { "type": "null" }
      ]},
    "c": { "type": "string" }
  }
  "required": ["v"]
}

pros

  • Standard json, not yet another format or extension.
  • No special parsers. Existing parser works (e.g JSON.parse())
  • Commentable on every value.
  • Metadata along with value (the schema only requires "v", "c" is does not even necessarily mean a comment here)

cons

  • More bytes.
  • Extra steps to put and extract values.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment