Skip to content

References

Greg Wicks edited this page Mar 5, 2018 · 7 revisions

To use references, there are three options, local references, external references to a specific value, and external references to a whole file. Refer to the testing directory for more examples. The syntax for each is below.

Local Reference

Simply replaces the value relative to the root of the object. Note that this is relative to the root of the current file, not the current object that reference is contained in.

input.json

{
    "a": "b",
    "c": { "$ref": "#/a" },
    "object": {
       "test": "bla",
       "another_ref": { "$ref": "#/object/test" }
    }
}

Output

{
    "a": "b",
    "c": "b",
    "object": {
       "test": "bla",
       "another_ref": "bla"
    }
}

External References

Note that when referring to external files, it is currently limited to local filesystem references. When parsing them, it will always parse any refs in those files recursively before merging the result into the main file.

Single Object

This uses the same syntax for object selection as above.

input.json

{
    "foo": { "$ref": "./bar.json#/baz" },
    "far": "test"
}

bar.json

{
    "baz": "bar",
    "c": "d"
}

Output

{
   "foo": "bar",
   "far": "test"
}

Full File

To include an entire file, simply reference it by path without any query on the end.

input.json

{
    "foo": { "$ref": "./bar.json" },
    "far": "test"
}

bar.json

{
    "baz": "bar",
    "c": "d"
}

Output

{
   "foo": {
      "baz": "bar",
      "c": "d"
   },
   "far": "test"
}
Clone this wiki locally