appendix C Introduction to JSON Pointer
JSON Pointer is introduced as a way to locate specific values inside a JSON document, and the appendix suggests practicing with a sample OpenAPI file from the project repository. The examples use the products-api.json document so you can see how pointer expressions select different parts of the file in a concrete API specification.
The syntax is built from tokens separated by slashes, with the whole pointer beginning with /. Simple pointers like /openapi, /info, and /info/title retrieve the OpenAPI version, the info object, and the API title respectively. The text also shows how special characters in path names must be escaped: ~ becomes ~0 and / becomes ~1, which is especially important when pointing into OpenAPI path keys that contain slashes.
JSON Pointer can also address array elements by numeric index, such as selecting the first parameter in a list. In addition to plain pointer strings, it can appear as a URI fragment identifier, which is useful when referencing values from a file-based JSON resource. The appendix notes that Spectral relies on this form of addressing for overrides, and it mentions command-line tools and online utilities that can be used to experiment with pointer expressions.
FAQ
What is JSON Pointer used for in the context of Spectral and API documents?
JSON Pointer is used to specify exactly which part of a JSON document should be targeted, such as when applying an override in Spectral. It provides a standardized way to locate values inside JSON structures.What standard defines JSON Pointer syntax?
JSON Pointer is defined in RFC 6901. The syntax uses a string made up of zero or more tokens, each prefixed by the/ character.How do you evaluate a JSON Pointer expression against a JSON file from the command line?
You can installjson-joy, which provides a json-pointer command-line tool. Then pipe the JSON document into the tool, for example: cat products-api.json | json-pointer /openapi.What does the pointer /openapi return for the example API document?
It returns the value of the openapi field, which is "3.0.3".What does the pointer /info return in the example document?
It returns the entire info object, which contains the API title and version:
{
"title": "An example API",
"version": "0.0.1"
}How do you access a nested property such as the API title using JSON Pointer?
Use a longer pointer path, such as/info/title. This returns "An example API".Why do path names in OpenAPI need special encoding in JSON Pointer?
Because the characters~ and / have special meaning in JSON Pointer syntax. They must be escaped when they appear inside a token: ~ becomes ~0 and / becomes ~1.How do you reference an OpenAPI path item such as /v1/products/{id} in JSON Pointer?
You must encode the slashes in the path token. For example, the path item is referenced as /paths/~1v1~1products~1{id}/delete/responses.How are array elements addressed in JSON Pointer?
Array elements are referenced by their numeric index. For example,/paths/~1v1~1products~1{id}/delete/parameters/0/in accesses the first parameter’s in value.Can JSON Pointer be written as a URI fragment as well as a plain string?
Yes. JSON Pointer can be used as a JSON string value or as a URI fragment identifier. For example:myapi.json#/paths/~1v1~1products~1{id}/delete/responses/204/description.
Automating API Delivery ebook for free