Issue
This is my file myfile.gz, it's a stream of objects like this:
{
"id": "12345",
"type": "A",
"count": 23,
"monday": {
"totalMoney": {
"categories": {
"FOOD": 80,
"CLOTHES": 23,
"TRAVEL": 0
}
},
"usedMoney": {
"categories": {
"FOOD": 70,
"CLOTHES": 20,
"TRAVEL": 10
}
},
"remainingMoney": {
"categories": {
"FOOD": 40,
"CLOTHES": 5,
"TRAVEL": 6
}
}
},
"tuesday": {
"totalMoney": {
"categories": {
"FOOD": 20,
"CLOTHES": 43,
"TRAVEL": 50
}
},
"usedMoney": {
"categories": {
"FOOD": 20,
"CLOTHES": 19,
"TRAVEL": 10
}
},
"remainingMoney": {
"categories": {
"FOOD": 55,
"CLOTHES": 5,
"TRAVEL": 69
}
}
},
"wednesday": {
"totalMoney": {
"categories": {
"FOOD": 990,
"CLOTHES": 443,
"TRAVEL": 550
}
},
"usedMoney": {
"categories": {
"FOOD": 220,
"CLOTHES": 193,
"TRAVEL": 110
}
},
"remainingMoney": {
"categories": {
"FOOD": 525,
"CLOTHES": 51,
"TRAVEL": 619
}
}
}, ...
}
{
"id": "54321",
"type": "B",
"count": 3,
"monday": {
"totalMoney": {
"categories": {
"FOOD": 80,
"CLOTHES": 23,
"TRAVEL": 0
}
},
"usedMoney": {
"categories": {
"FOOD": 70,
"CLOTHES": 20,
"TRAVEL": 10
}
},
"remainingMoney": {
"categories": {
"FOOD": 40,
"CLOTHES": 5,
"TRAVEL": 6
}
}
},
"tuesday": {
"totalMoney": {
"categories": {
"FOOD": 20,
"CLOTHES": 43,
"TRAVEL": 50
}
},
"usedMoney": {
"categories": {
"FOOD": 20,
"CLOTHES": 19,
"TRAVEL": 10
}
},
"remainingMoney": {
"categories": {
"FOOD": 55,
"CLOTHES": 5,
"TRAVEL": 69
}
}
},
"wednesday": {
"totalMoney": {
"categories": {
"FOOD": 990,
"CLOTHES": 443,
"TRAVEL": 550
}
},
"usedMoney": {
"categories": {
"FOOD": 220,
"CLOTHES": 193,
"TRAVEL": 110
}
},
"remainingMoney": {
"categories": {
"FOOD": 525,
"CLOTHES": 51,
"TRAVEL": 619
}
}
}, ...
} ...
I want to select and return the object ID which is of type S, count > 20, and remainingMoney for FOOD inside categories on a friday is the least among all the other objects. This is my first time using jq so I am super lost and confused on how to achieve this. Any help is appreciated. Thanks!
Solution
Use -s
to turn the stream into an array, then map(select(…))
to filter by your criteria, find the minimum using min_by(…)
with your other criteria, and finally output your desired field .id
. Add parameter -r
(next to -s
) if you want the output as raw text instead of JSON strings.
jq -s '
map(select(.type == "S" and .count > 20))
| min_by(.friday.remainingMoney.categories.FOOD)
| .id
'
Answered By - pmf Answer Checked By - Willingham (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.