brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: jq select() filters JSON in the terminal by a field value

Terminal
~/tips/tip-terminal-jq-select-filter

echo '[{"n":"a","up":true},{"n":"b","up":false}]' | jq -r '.[] | select(.up) | .n'

Keeps only the JSON items that match a condition and prints one field.

Filtering a JSON list with grep breaks easily, and a jq filter does not.

echo '[{"n":"a","up":true},{"n":"b","up":false}]' | jq -r '.[] | select(.up) | .n'

.[] walks over the array, select(.up) keeps items where the condition is true, and .n picks one field. Compare values with select(.n == "b"). -r prints raw strings without quotes. Use -c to print each result on one line. The filter is a program and has to be quoted with single quotes in the shell.

Further reading: jq manual

More Terminal tips ioreg -rn AppleSmartBattery | grep CycleCount Terminal can save your windows and tabs as a window group Export a Terminal profile to a .terminal file and import it on another Mac mc hidutil property --set '{"UserKeyMapping":[{"HIDKeyboardModifierMappingSrc":0x700000039,"HIDKeyboardModifierMappingDst":0x700000029}]}' All Terminal tips (116) All topics

Link