300-910 · Question #125
Refer to the exhibit. An engineer must find application log file entries when a user named user1 makes an HTTP GET request and return only the time of the query. Which command completes the Bash scrip
The correct answer is B. jq 'select(.user=="user1" and .method=="GET").time' app.log. To filter JSON log entries for a specific user and HTTP method, then extract only the timestamp, the jq command must use select for conditional filtering and then dot notation to project the desired field.
Question
2021-11-12 20:13:44 2021-11-12 20:14:44
Options
- Ajq '.time' app.log
- Bjq 'select(.user=="user1" and .method=="GET").time' app.log
- Cjq 'select(.user=="user1" and .method=="GET")' app.log
- Djq 'select(.user=="user1" and .GET).time' app.log
How the community answered
(43 responses)- A9% (4)
- B81% (35)
- C2% (1)
- D7% (3)
Why each option
To filter JSON log entries for a specific user and HTTP method, then extract only the timestamp, the `jq` command must use `select` for conditional filtering and then dot notation to project the desired field.
`jq '.time' app.log` would extract the `time` field from every log entry without applying any filters for user or method, which does not match the requirement.
The `jq 'select(.user=="user1" and .method=="GET").time' app.log` command correctly uses `select` to filter for objects where the `user` field is "user1" AND the `method` field is "GET", then uses `.time` to project only the value of the `time` field for the selected entries, matching the desired output.
`jq 'select(.user=="user1" and .method=="GET')' app.log` would correctly filter the log entries but would return the entire JSON object for matching entries, not just the `time` field as required.
`jq 'select(.user=="user1" and .GET).time' app.log` incorrectly attempts to filter for `.GET` as a property rather than checking the value of the `method` property, which would result in incorrect filtering or a syntax error.
Concept tested: JSON parsing and filtering with jq.
Source: https://stedolan.github.io/jq/manual/
Topics
Community Discussion
No community discussion yet for this question.