liveBook · Manning

JSON path language

As explained in Automating API Delivery

A JSONPath expression is a query expression that selects or navigates elements in a JSON document.

An API linting rule needs to find the right part of a definition before it can check anything. JSONPath is a query language that selects data from a JSON document. An expression starts with `$`, the document root, then navigates through child elements using dots or brackets. For example, `$.info.version` selects the version value from an OpenAPI definition’s `info` object. section 3.6

In a Spectral rule, `given` holds the JSONPath expression that selects where the rule applies; `then` specifies the check. The expression can select zero or more elements. If it selects none, the rule is not evaluated—an important distinction when deciding whether a missing element should itself be reported. section 3.6

Listing 3.1 A Spectral rule to check that info.description is present
rules:
  info-description-is-required:      #1
    message: "API definition files must 
 have info.description defined"        #2
    severity: error                  #3
    given: $.info                 #4
    then:
      field: description      #5
      function: truthy      #6

This rule selects `$.info`, then checks its `description` field with Spectral’s `truthy` function. Selecting the containing object lets the rule report a missing description. section 3.6

Some checks need more than basic JSONPath. To check whether every URI path starts with a version number, Spectral uses the JSONPath Plus expression `$.paths[*]~` to select the path names. The rule’s `pattern` function then checks those names against `^/v\d+/.*$`. To inspect the selection before linting, convert the OpenAPI YAML file with `yq eval -o=json product-catalog-v3.oas.yaml > product-catalog-v3.oas.json`, then run `jpp "$.paths[*]~" product-catalog-v3.oas.json`; `jpp` prints the matching paths. section 3.7

Targeting GET operation IDs

A custom Spectral rule can use `$.paths[*].get.operationId` as its `given` expression. This selects GET operation IDs for a custom function that checks their naming convention, rather than applying the function to every field in the definition. section 10.1.3

How Automating API Delivery covers it

How deep it goes
Defines root and child navigation, then works through rule targets and JSONPath Plus path-name selection without developing a full query-language reference.
Examples
Uses `$.info`, `$.paths[*]~`, and `$.paths[*].get.operationId` to target descriptions, URI paths, and GET operation IDs.
What it assumes
Assumes familiarity with API definitions while teaching JSONPath’s root and child-navigation syntax from basics.
What you can do after
Apply it via Spectral to target linting checks, and test JSONPath Plus selections with `jpp` before running a rule.
How it connects
Connects JSONPath to OpenAPI, Spectral rulesets, API linting, JSONPath Plus, and custom linting functions.

Also explained in

Compare how 3 books explain JSON path language →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Automating API Delivery. The section links point to the parts of the book it draws on.