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: redis-cli --json prints replies as JSON that jq can read

CLI
~/tips/tip-redis-cli-json-output

redis-cli --json HGETALL user

Formats Redis replies as JSON, so a hash becomes an object you can pipe straight into jq.

The default redis-cli output is made for people: numbered lists, quotes, type hints. Once you pipe it into another tool, you end up parsing text. Newer redis-cli versions can just print JSON.

redis-cli --json HGETALL user

The usage text lists --json as “Output in JSON format (default RESP3, use -2 if you want to use with RESP2)”. That RESP detail changes the shape of the result. In a throwaway instance with a hash called user (fields name and city), redis-cli 7.0.15 printed:

redis-cli --json HGETALL user
{"name":"Anna","city":"Leiden"}

redis-cli -2 --json HGETALL user
["name","Anna","city","Leiden"]

With RESP3 you get an object, with RESP2 a flat list of alternating names and values. A missing key printed null.

redis-cli --json HGETALL user | jq -r .city

jq takes it from there. Lists work the same way: redis-cli --json LRANGE mylist 0 -1 | jq -r '.[1]' printed the second element, including one with a space in it.

There is also --quoted-json, which the help describes as the same thing with ASCII-safe quoted strings instead of Unicode. And --csv for spreadsheet-style output. The docs say --csv works on a single command only, not as an export of a whole database. Neither option replaces a proper backup.

Caveat: check that your redis-cli has the option before putting it in a script. redis-cli --help | grep json shows it on 7.0.15, the version used above.

Further reading: Redis CLI: usage and CSV output

More CLI tips wsl --shutdown wsl --distribution <Distribution Name> --user <User Name> Compress-Archive -Path C:\Reference -DestinationPath C:\Archives\Draft.zip wsl --terminate <Distribution Name> rg -o -r '$2' '"(GET|POST) (\S+)' access.log All CLI tips (60) All topics

Link