Closed conikeec closed 1 year ago
@conikeec, to filter on a sibling you have to make a query at a higher level to be able to get the parent object. Queries can be nested.
So in your case you have to query for category items that have a
subCategory
key which has at least one item starting with the word CRM
.
And you need to add a #
at the end of the outermost query to return all
matching items, not just the first. See also [1]
That will return a list of category items that matches your criteria.
Only at thay point you can get the category
field of them, resulting in a
list of categories.
So with query:
categories.#(subCategory.#(%"CRM*"))#.category
You get:
["Sales"]
I hope that helps.
[1] https://github.com/tidwall/gjson/blob/master/SYNTAX.md#queries
Thank you very much @volans- . It works. Question: Is there a provision for case-insensitive search
Not that I know of, I don't think there is regex support, judging also from issue #135.
But if the possible variants are limited you could achieve the same results
using multipaths [1] combined with the @flatten
modifier [2] to get a
flat list:
[categories.#(subCategory.#(%"CRM*"))#.category,categories.#(subCategory.#(%"crm*"))#.category].@flatten
This might lead to duplicate results if the items match both queries.
Another alternative could be to create your own custom modifier [3] or do multiple queries and mangle the data in your code.
[1] https://github.com/tidwall/gjson/blob/master/SYNTAX.md#multipaths [2] https://github.com/tidwall/gjson/blob/master/SYNTAX.md#modifiers [3] https://github.com/tidwall/gjson/blob/master/SYNTAX.md#custom-modifiers
@conikeec sorry for the bad formatting, apparently GitHub email reply support is fairly limited, I can't even edit the replies to add markdown from the browser now. I hope it's still understandable.
Thanks again @volans- . I figured it's more optimal to lowercase the loaded JSON and search string. 😄
Another quick question:
I revised the JSON above to add another field skucode
..
Using the same query, can I fetch both the siblings - category
and skucode
, when subCategory
matches ?
@conikeec yes, you can leverage again the multipaths, that can be used anywhere in the query and apply to the current object.
So with an input of:
{
"categories": [
{
"category": "Sales",
"skucode": "A001",
"subCategory": [
"CRM Software",
"Another category"
]
},
{
"category": "Excluded",
"skucode": "A002",
"subCategory": [
"NOT CRM Software",
"Another category"
]
},
{
"category": "Communication",
"skucode": "A003",
"subCategory": [
"CRM Software",
"Another category"
]
}
]
}
You can get a list of objects with the query:
categories.#(subCategory.#(%"CRM*"))#.{category,skucode}
that returns:
[{"category":"Sales","skucode":"A001"},{"category":"Communication","skucode":"A003"}]
You can customize the key names with:
categories.#(subCategory.#(%"CRM*"))#.{"cat":category,"sku":skucode}
that returns:
[{"cat":"Sales","sku":"A001"},{"cat":"Communication","sku":"A003"}]
Or just decide to get a list of 2 items lists with:
categories.#(subCategory.#(%"CRM*"))#.[category,skucode]
that returns:
[["Sales","A001"],["Communication","A003"]]
@volans- appreciate the detailed reply. Again, thank you as this is what i was looking for.
Closing the issue
Hi
I am trying to extract the sibling node value based on a wild card search and not able to get it to work. Wondering if my query syntax is the issue