JQ Flashcards1. View JSON in a Readable FormatQuestion: What command is used to pretty-print JSON content?
Answer: jq '.' sample_nows.json
2. Read the First JSON ElementQuestion: How do you extract the first object from a JSON array?
Answer: jq 'first(.[])' sample_nows.json
3. Read the Last JSON ElementQuestion: What command retrieves the last JSON element?
Answer: jq 'last(.[])' sample_nows.json
4. Read the Top 3 JSON ElementsQuestion: How do you extract the first three elements of a JSON array?
Answer: jq 'limit(3;.[])' sample_nows.json
5. Read the 2nd and 3rd ElementsQuestion: How can you extract elements from index 2 to 3 in JSON (Python-style slicing)?
Answer: jq '.[2:4]' sample_nows.json
6. Extract Specific FieldsQuestion: How can you extract the balance and age fields from each JSON object?
Answer: jq '.[] | [.balance,.age]' sample_nows.json
7. Perform Calculations on JSON DataQuestion: How do you calculate the years left until age 65 for each person?
Answer: jq '.[] | [.age, 65 - .age]' sample_nows.json
8. Convert JSON to CSVQuestion: What command converts JSON data into CSV format?
Answer: jq '.[] | [.company, .phone, .address] | @csv' sample_nows.json
9. Convert JSON to TSVQuestion: How do you output JSON data in a tab-separated format?
Answer: jq '.[] | [.company, .phone, .address] | @tsv' sample_nows.json
10. Use Custom DelimitersQuestion: How do you output JSON data with a custom pipe (|) delimiter?
Answer: jq '.[] | [.company, .phone, .address] | join("|")' sample_nows.json
11. Convert Numbers to Strings and Format OutputQuestion: How do you convert the age field to a string before joining values?
Answer: jq '.[] | [.balance,(.age | tostring)] | join("|")' sample_nows.json
12. Process Nested Arrays and Extract NamesQuestion: How do you extract all friend names from a JSON array?
Answer: jq '.[] | [.friends[].name]' sample_nows.json
13. Parse Multi-Level FieldsQuestion: How do you extract the first and last names from JSON objects?
Answer: jq '.[] | [.name.first, .name.last]' sample_nows.json
14. Query JSON Based on ConditionsQuestion: How do you filter JSON elements where index is greater than 2?
Answer: jq 'map(select(.index > 2))' sample_nows.json
15. Sorting JSON ElementsQuestion: How do you sort JSON data by age in ascending and descending order?
Answer:
Ascending: jq 'sort_by(.age)' sample_nows.json
Descending: jq 'sort_by(-.age)' sample_nows.json
16. Use CasesQuestion: How can you fetch and parse GitHub status using JQ?
Answer:
curl -s https://www.githubstatus.com/api/v2/status.json | jq '.'Question: How do you extract only the status field from the GitHub status API?
Answer:
curl -s https://www.githubstatus.com/api/v2/status.json | jq '.status'