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 del() removes a key from JSON, like a password before you share a config

Json
~/tips/tip-jq-del-remove-key

jq 'del(.password)' config.json

Removes chosen keys or array items from JSON, so you can share a config without secrets.

Before I paste a config file into a forum post or a GitHub issue, I want the secrets gone, and doing that by hand in an editor is how a token slips through. jq has a builtin for it, del(), which removes a key and its value and prints the rest of the JSON.

jq 'del(.password)' config.json

That prints config.json without the top-level password key. The argument is a path, so nested keys work, and you can list more than one with a comma:

jq 'del(.db.password, .smtp.token)' config.json > config.shared.json

Now config.shared.json has everything except those two values, and the original stays as it was. Arrays work the same way with an index: the manual’s example del(.[1, 2]) on ["foo", "bar", "baz"] leaves just ["foo"].

I’d reach for this for the JSON settings files that many self-hosted apps keep in their appdata folder, something like /mnt/user/appdata/<app>/config.json, when you want to show someone your setup without your API keys.

The caveat is about the shell. jq never changes the file, it writes to standard output. So don’t try jq 'del(.password)' config.json > config.json: the shell empties config.json before jq even reads it, and you’re left with nothing. Write to a new file and move it over the old one if that’s what you want. And check the result before you share it, because del() only removes the keys you name. A secret under a key you forgot, or inside a URL string, is still in there.

Further reading: jq 1.8 manual

More Json tips echo '{"id":1,"payload":"{\"user\":\"ann\",\"tags\":[\"x\",\"y\"]}"}' | jq -c '.payload | fromjson | .tags' jq empty config.json Paths() tells you where in a big JSON document a value lives jq -c --slurpfile u users.json ' jq -cn --stream 'fromstream(1|truncate_stream(inputs))' big.json All Json tips (9) All topics

Link