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 fromjson opens JSON that was stored inside a string

Json
~/tips/tip-jq-fromjson-nested-string

echo '{"id":1,"payload":"{\"user\":\"ann\",\"tags\":[\"x\",\"y\"]}"}' | jq -c '.payload | fromjson | .tags'

Parses a string field that itself contains JSON, so you can filter into it.

Some APIs and log lines hand you JSON inside JSON: a field whose value is a string full of escaped quotes. Reading that by eye is no fun. fromjson unwraps it.

echo '{"id":1,"payload":"{\"user\":\"ann\",\"tags\":[\"x\",\"y\"]}"}' | jq -c '.payload | fromjson | .tags'
["x","y"]

The string in .payload is parsed as JSON, and from there you filter like on any other document. To unwrap it in place and keep the rest, use an update:

jq '.payload |= fromjson' event.json

The opposite direction is tojson, which turns any value into a string. [1,2] | tojson gives "[1,2]", handy when an API wants JSON stored as text in a field.

The caveat is that bad input is a hard error. A payload of {oops stops jq with Invalid numeric literal and exit code 5. If some records are broken, wrap the call in try, as in (.payload | try fromjson), which skips the failing one.

I would reach for this first with webhook payloads and log entries, since both love to store a whole event as text.

The in-place update works on the earlier example too. On {"id":1,"payload":"{\"user\":\"ann\"}"}, jq -c '.payload |= fromjson' prints {"id":1,"payload":{"user":"ann"}}. On a list with one good and one broken string, .[] | .p | try fromjson prints the good one and says nothing about the broken one.

Further reading: jq manual

More Json tips 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 const text = '{"createdAt":"2026-11-30T09:00:00Z","name":"x"}'; All Json tips (9) All topics

Link