Reputation: 2565
I have the following json Object:
{
"foo": {
"name": "Name 1",
"color": "green",
"something_else": {
"name" : "Name 2"
}
},
"bar": {
"name": "Something else",
"color": "red"
}
}
To get all possible parents properties of the property called "name" using jq I tried :
path(recurse|select(.name? !=""))[0]
And it works and give back :
"foo"
"foo"
"bar"
Now I want to apply regex to filter the property value, say I want to consider only all properties called name
that have a value beginning with "Name" and followed by a number like "Name 2"
, to get:
"foo"
"foo"
I tried this:
path(recurse|select(.name? =~ match(/Name */)))[0]
How to use match and how to place it correctly inside the query ?
Upvotes: 14
Views: 24206
Reputation: 85780
You could use paths/1
instead of path
because the former ignores null paths. Also with path
you need to add a filter logic to ignore the null
which does not match any of the regex conditions
paths(select(.name? | match("Name [0-9]")))[0]
See jq - documetation - paths/1
Upvotes: 26