What are JSONPath expressions?
JSONPath expressions provide a powerful tool for querying and manipulating JSON data in your OpenAPI documents. By using JSONPath, you can selectively target specific nodes in your API spec and apply modifications without altering the base structure.
Example JSONPath expressions
- Target all POST or GET operations
- Target operations with a request body
- Target operations with a specific visibility notation
- Inject annotations as siblings
Target all POST or GET operations
This expression selects all paths that contain either POST or GET operations. It’s useful when you want to apply changes or add annotations specifically to these HTTP methods.
JSONPath target
$.paths.*[?("post","get")]
Overlay action
actions:- target: $.paths.*[?("post","get")]update:x-apiture-logging: truedescription: "Logging enabled for all POST and GET operations."
Target operations with a request body
This expression targets all operations that include a request body. It’s ideal for adding descriptions, examples, or additional schema validations to request bodies.
JSONPath target
$.paths.*[?(@.requestBody)]
Overlay action
actions:- target: $.paths.*[?(@.requestBody)]update:x-custom-validation: "custom-validation-schema"description: "Custom validations applied to request bodies."
Target operations with a specific visibility notation
This expression is used to find all operations marked with ‘admin’ visibility. It can be used to apply additional security measures or modify documentation for admin-only endpoints.
JSONPath target
$.paths.*[?(@["x-visibility"] == "admin")]
Overlay action
actions:- target: $.paths.*[?(@["x-visibility"] == "admin")]update:x-custom-security: "enhanced"description: "Enhanced security for admin endpoints."
Inject annotations as siblings
This expression targets all operations within an OpenAPI document that have an operationId
property.
JSONPath target
$.paths.*.*[?(@.operationId)]
Overlay action
actions:- target: $.paths.*.*[?(@.operationId)]update:x-detailed-logging: "enabled"log_level: "verbose"description: "Verbose logging enabled for this operation to track detailed performance metrics."