liveBook · Manning

JSON Pointer

As explained in The Design of Web APIs, Second Edition

JSON Pointer is a standardized string syntax for identifying a specific value within a JSON document.

JSON Pointer is a standard way to identify a value in a JSON or YAML document. In an OpenAPI document, a $ref can use a pointer to locate a reusable schema instead of repeating that schema in every operation. The same definition can then serve multiple responses, reducing duplication and unwanted differences. section 7.7.1

Share a product response schema

Both POST /products and GET /products/{productReference} can set their successful response schema to $ref: "#/components/schemas/Product". Here, # identifies the document root; each slash-separated segment names the next property. The pointer locates components.schemas.Product. Quote it in YAML because # otherwise starts a comment. section 7.7.1

Listing 7.12 References to reusable schemas
...
paths:
  /products:
    ...
    post:
      ...
      responses:
        "201":
          ...
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Product"    #1

  ...
  /products/{productReference}:
    get:
     ...
      responses:
        "200":
          ...
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Product"   #1

...
components:
  schemas:
    Product: ...    #1

Compare the two response references with the single Product definition under components.schemas. section 7.7.1

Pointers are useful beyond response schemas. OpenAPI references can target reusable parameters, request bodies, headers, and responses; deeper references can target an inner schema without creating another small component. Reuse improves consistency, but optimize only where the benefit outweighs the added complexity. section 17.8

A pointer can also locate a problem rather than a reusable definition. A machine-readable API error may include an optional pointer to the offending place in a request body. In Spectral overrides, a pointer identifies where a lint rule should be ignored: #/paths/~1accounts/get/parameters identifies the parameters of GET /accounts, with ~1 standing for the slash in the path name. section 9.8.4 section 18.9.2

How The Design of Web APIs, Second Edition covers it

How deep it goes
Explains the root and slash-separated property segments through a worked schema reference, then shows slash escaping in a lint override.
Examples
Shows shared Product response schemas, an optional request-body error location, and a Spectral override for GET /accounts parameters.
What it assumes
Assumes familiarity with OpenAPI response schemas and components while explaining pointer roots and property segments.
What you can do after
Apply it via OpenAPI $ref to reuse schemas and via Spectral overrides to target a specific lint problem.
How it connects
Connects JSON Pointer to OpenAPI, JSON Schema, reusable components, machine-readable errors, and API linting.

Also explained in

Compare how 2 books explain JSON Pointer →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Design of Web APIs, Second Edition. The section links point to the parts of the book it draws on.