appendix C Introduction to JSON Pointer

 

This appendix introduces JSON Pointer, and you can work through the examples here using the project repository you cloned in appendix B. Navigate to the appendix folder in the project to find the products-api.json file you need for this.

Spectral uses the JSON Pointer syntax to specify the part of a JSON document to apply an override to. JSON Pointer is a syntax for locating a value within a JSON document, and the specification is defined in RFC6901. The syntax is a string of zero or more tokens prefixed by the “/” character. You can see this in action by evaluating JSON Pointer expressions on a JSON document. To do this, install json-joy (https://github.com/streamich/json-joy), which gives you a json-pointer command-line tool for testing JSON Pointer expressions:

npm install -g json-joy

Now you’re given the following products-api.json document in the appendix folder:

{
  "openapi": "3.0.3",
  "info": {
    "title": "An example API",
    "version": "0.0.1"
  },
  "paths": {
    "/v1/products/{id}": {
      "delete": {
        "parameters": [
          {
            "in": "path",
            "name": "id",
            "required": true,
            "schema": {
              "type": "integer"
            }
          }
        ],
        "responses": {
          "204": {
            "description": "Product deleted."
          }
        }
      }
    }
  }
}

To evaluate pointer /openapi, run the following command: