Reputation: 439
Using jq, I am searching through a large json file that has objects that contain multiple year values in a attribute. I want to be able to narrow my search and only retrieve objects that are greater or less(older or younger) than YEAR(example 2015). This is tricky because the attributes can have multiple year values, some greater and less than the year I am searching for.
Here is a example of the code I am searching through. It's all basically this.
cat stackover.json
{
"tokens": [
{
"Name": "stack overflow",
"year": [
"1997",
"1998",
"1997"
]
},
{
"Name": "Return pizza",
"year": [
"1998",
"2015",
"2014",
"1998"
]
}
]
}
I know how to search for a contains one of the values... but can figure out how to search all the values and determine if all of them are less than 2000.
If I search for less than '<', then I get no out put back. If I search for greater than '>', then I get back both object multiple times
cat stackover.json | jq '.tokens[] | select(.year[] > 2000)'
{
"Name": "stack overflow",
"year": [
"1997",
"1998",
"1997"
]
}
{
"Name": "stack overflow",
"year": [
"1997",
"1998",
"1997"
]
}
{
"Name": "stack overflow",
"year": [
"1997",
"1998",
"1997"
]
}
{
"Name": "Return pizza",
"year": [
"1998",
"2015",
"2014",
"1998"
]
}
{
"Name": "Return pizza",
"year": [
"1998",
"2015",
"2014",
"1998"
]
}
{
"Name": "Return pizza",
"year": [
"1998",
"2015",
"2014",
"1998"
]
}
{
"Name": "Return pizza",
"year": [
"1998",
"2015",
"2014",
"1998"
]
}
What I am trying to accomplish.
If I search for anything less than 2000, I want a query that will only return "stack overflow" since it does not have a 20XX value in the available values.
cat stackover.json | code searching for only objects(stack overflow) where all values are less than 2000.
{
"Name": "stack overflow",
"year": [
"1997",
"1998",
"1997"
]
}
Upvotes: 3
Views: 8526
Reputation: 116780
In brief, all/2
is your friend.
Assuming we have defined $mx (a string) as the cutoff year, a suitable filter would be:
.tokens[] | select( all(.year[]; . < $mx) )
The command-line option '--arg mx N' interprets N as a string, so a suitable invocation would be like so:
jq --arg mx 2000 -f filter.jq stackover.json
(Notice that we can avoid converting the dates to numbers by letting $mx be a string.)
Upvotes: 6