JSON Processing in Shell
Level 5252 keystrokes
jq parses JSON in shell scripts: here it formats each user as text with string interpolation, counts the users, and builds a new array with map(select(...)) that keeps only users older than 26.
Shell code
# Sample JSON with a users array1json='{"users":[{"name":"Alice","age":30},{"name":"Bob","age":25}]}'# .users[] emits each user; \(...) inserts values into the string; -r prints without quotes2echo "$json" | jq -r '.users[] | "\(.name): \(.age) years"'# Capture the array length (2) in a shell variable3count=$(echo "$json" | jq '.users | length')4echo "Total users: $count"# map(select(...)) keeps users with age > 26 and prints the array as indented JSON5echo "$json" | jq '.users | map(select(.age > 26))'Output
Alice: 30 years
Bob: 25 years
Total users: 2
[
{
"name": "Alice",
"age": 30
}
]Practice typing this code. Results are not recorded in the rankings.
Symbols in this code
The symbols you type in this Shell code and how often they appear.
"× 24'× 8:× 7.× 6|× 6(× 5)× 5$× 5,× 3{× 3