matching keys with regex #37
-
Hi Is there a way to match regex against keys using the extended syntax? I'm trying to select a funny key from an OpenAPI document: paths:
/path:
get:
responses:
200:
content:
application/json; version=2.4.49:
schema:
$ref: "#/components/schemas/MyResponse" I want to select all content nodes with such child (the version changes over time, thus regex)
|
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 10 replies
-
Hi @rafalkrupinski, The search filter function and current key identifier ( I've added an extra "content" property in this example to show that the regex filter is only selecting objects with a property name containing "version=". import { jsonpath } from "json-p3";
const data = JSON.parse(`
{
"paths": {
"/path": {
"get": {
"responses": {
"200": {
"content": {
"application/json; version=2.4.49": {
"schema": {
"$ref": "#/components/schemas/MyResponse"
}
}
}
}
}
}
}
},
"content": {
"foo": {
"schema": {
"$ref": "#/components/schemas/MyResponse"
}
}
}
}`);
const env = new jsonpath.JSONPathEnvironment({ strict: false });
const nodes = env.query(`$..content[?search(#, "version=")]`, data);
console.log(JSON.stringify(nodes.values(), undefined, " ")); Output [
{
"schema": {
"$ref": "#/components/schemas/MyResponse"
}
}
] (aside: I've just noticed this works without setting |
Beta Was this translation helpful? Give feedback.
-
This alternative query selects objects with a
Output [
{
"content": {
"application/json; version=2.4.49": {
"schema": {
"$ref": "#/components/schemas/MyResponse"
}
}
}
}
] But this only works if |
Beta Was this translation helpful? Give feedback.
-
How to get [
{
"application/json; version=2.4.49": {
"schema": {
"$ref": "#/components/schemas/MyResponse"
}
}
}
] as the output :) No, there may be more properties in content. |
Beta Was this translation helpful? Give feedback.
-
{
"responses": {
"200": {
"description": "OK",
"content": {
"application/json; version=1": {
"schema": {}
},
"application/json": {
"schema": {}
}
}
}
}
}
result (paths)
for json path
result [
"$.responses['200'].content['application/json; version=1']"
] how to get [
"$.responses['200'].content"
] BTW, it's really cool that nested filters work |
Beta Was this translation helpful? Give feedback.
Probably not the most intuitive solution, but here's a prototype implementation of
has
(in TypeScript, some of these helper functions are not currently exported)..