Multiple conditions and nested data
Hi. I would like to get help with this query.
if company_info.active = TRUE and company_info.company_employees.salary > 4200
The response im searching for is this:
[
{
"company_id": 3,
"company_info": {
"active": true,
"company_employees": [
{
"id": "1",
"name": "Charlotte Doe",
"salary": "4500"
},
{
"id": "2",
"name": "Chris Doe",
"salary": "5200"
}
]
}
}
]
Here is example from the full object.
[
{
"company_id": 1,
"company_info": {
"active": true,
"company_employees": [
{
"id": "1",
"name": "John Doe",
"salary": "2500"
},
{
"id": "2",
"name": "Liza Doe",
"salary": "3200"
}
]
}
},
{
"company_id": 2,
"company_info": {
"active": false,
"company_employees": [
{
"id": "1",
"name": "Jacob Doe",
"salary": "6500"
},
{
"id": "2",
"name": "Hanna Doe",
"salary": "8200"
}
]
}
},
{
"company_id": 3,
"company_info": {
"active": true,
"company_employees": [
{
"id": "1",
"name": "Charlotte Doe",
"salary": "4500"
},
{
"id": "2",
"name": "Chris Doe",
"salary": "5200"
}
]
}
}
]
I am trying to do the same without any luck. Did you manage to get this working?
The problem is not fully specified. Anyway, if you want the companies where any of the employees have a salary above 4200:
[?company_info.active && max(company_info.company_employees | [].salary) > `4200`]
If you want the companies where all of the employees have a salary above 4200:
[?company_info.active && min(company_info.company_employees | [].salary) > `4200`]
@bjorne , my issue is slightly different. For example, given:
{
"a": {"v1": 100},
"b": {"v1": 200},
"c": {"v1": 100}
}
I want all the subdictionaries where v1>100.
@dbarrosop How about this?
values(@)[?v1 > `100`]
That works great, is it possible to extract the key as well? Or instead, both would work.
@dbarrosop Did you ever figure out the way to extract the key, or the key and value both?
Unfortunately not.
@dbarrosop I had a similar problem working with OpenAPI3 definitions. In particular the paths of an OpenAPI def are normalized so that it is difficult to use jmespath to extract both the key and value of a searched for item i.e. the path object for a particular operationId. I have a suspicion that there is some complex query that might do what is needed but was unable to find it. Instead I solved it by creating a custom function (named items) which returns a list of objects containing key and value items. Once I had this it was possible to search the value for the required condition and return the objects I wanted.
Here is the custom items function (python):
class CustomFunctions(functions.Functions):
@functions.signature({'types': ['object']})
def _func_items(self, s):
result = []
for key, value in s.items():
result.append({'key': key, 'value': value})
return result
using this function a search that would return both key and value would be:
items(@) | [?value.v1 > `100`]