Searching Large JSON Files Interactively
When a JSON file is too big to scroll through, fx opens it in an interactive terminal viewer where you can regex-search values, fuzzy-jump to keys, and print just the part you need. Install fx and follow along.
The examples use this services.json:
{
"region": "eu-west-1",
"services": [
{"name": "auth", "status": "healthy", "replicas": 3, "config": {"timeout": 30, "db": {"host": "auth-db.internal", "port": 5432}}},
{"name": "billing", "status": "degraded", "replicas": 2, "config": {"timeout": 60, "db": {"host": "billing-db.internal", "port": 5432}}, "lastError": "Timeout connecting to payment gateway"},
{"name": "search", "status": "healthy", "replicas": 5, "config": {"timeout": 15, "cache": {"host": "redis.internal", "port": 6379}}},
{"name": "notifications", "status": "down", "replicas": 0, "config": {"timeout": 30, "queue": {"host": "mq.internal", "port": 5672}}, "lastError": "Connection refused: mq.internal:5672"}
]
}Open a Large JSON File in the Terminal
Pass the file, or pipe JSON in, without any other arguments:
fx services.jsoncurl -s https://api.example.com/services | fxMove with the arrow keys or h j k l. Press ? for the full list of keys and q to quit. If you pass a JavaScript argument, fx prints the result instead of opening the viewer.
Search JSON with a Regex
Press /, type a regex pattern, and press enter. Press n to go to the next match and N to go back to the previous one.
/refusedThis jumps to "Connection refused: mq.internal:5672". Search matches keys and values, so /host finds every host key and /5432 finds both database ports.
Search is case-insensitive by default. To change that:
- Case-sensitive: add
/at the end of the pattern, e.g./[a-z]/. - Case-insensitive (explicitly): add the
/iflag, e.g./[a-z]/i.
So /Timeout/ matches "Timeout connecting to payment gateway", but not the lowercase timeout keys.
Find a Key in a Big JSON File by Path
Press @ to fuzzy-search JSON paths and jump to the first match. Type a few characters of the key names, such as queue, to reach .services[3].config.queue without knowing the array index.
If you already know the path, press . to open the query input and type it. fx suggests keys as you type; press tab to accept the suggestion. For example, type .services[1].config.db and press enter to show only the billing database settings. Press esc to return to the whole document.
After a jump, press [ to go back to your previous location and ] to go forward again.
Collapse and Expand All Nodes
These keys (from Key Bindings) help you get an overview of a large document:
| Key(s) | Action |
|---|---|
| E / e | collapse all / expand all |
| 1-9 | collapse to nth level |
| ←, h / →, l | collapse / expand |
| H, shift+← / L, shift+→ | collapse / expand recursively |
| J, shift+↓ / K, shift+↑ | next / previous sibling |
| g, home / G, end | go to top / bottom |
| space / b, d / u | page down / up, half page |
A useful pattern: press E to collapse everything, expand down to the array you care about, move between items with J / K, then press L to expand only the item you want.
Preview Long String Values
Long strings such as stack traces or embedded documents get cut off in the tree. Press p on a value to open it in preview mode, or press z to toggle string wrapping in place. Press y to yank (copy) the value.
Extract Part of a JSON File from the Viewer
fx draws its interface on stderr, so stdout stays free for data. Redirect stdout to a file, navigate to the node you want, and press P to print it:
fx services.json > billing-db.jsonNavigate to .services[1].config.db, press P, and billing-db.json contains:
{
"host": "billing-db.internal",
"port": 5432
}Once you know the path, the same extraction works without the viewer:
fx services.json '.services[1].config.db'{
"host": "billing-db.internal",
"port": 5432
}Show Sizes, Line Numbers, and Start Collapsed
Set these environment variables in your shell profile:
export FX_COLLAPSED=true # start with nodes collapsed
export FX_SHOW_SIZE=true # show the number of elements in arrays and objects
export FX_LINE_NUMBERS=true # show line numbersStarting collapsed is useful for very large files: you see the top-level keys first and expand only what you need. Inside the viewer, press s s to toggle sizes and s l to toggle line numbers.
Filter JSON from the Command Line Instead
Interactive search finds one match at a time. When you want every match, or need the result in a script, filter with the CLI. ? filters an array, and @ maps over it:
fx services.json '.services' '?.status !== "healthy"' '@.name'[
"billing",
"notifications"
]Use a JavaScript regex inside the filter for pattern matching:
fx services.json '.services' '?/timeout/i.test(x.lastError)' '@.name'[
"billing"
]To find records that contain a string anywhere, at any depth, search the serialized record:
fx services.json '.services' '?JSON.stringify(x).includes("mq.internal")' '@.name'[
"notifications"
]Pipe a filtered result back into fx to browse only the matches interactively:
fx services.json '.services' '?.status !== "healthy"' | fxOr run the same filter inside the viewer: press ., type .services ?.status !== "healthy", and press enter. See Interactive Query.
List Every Path Where a Key Appears
To answer "where is this key used in the file?", walk the document and collect matching paths:
fx services.json 'x => {
const found = []
const visit = (v, path) => {
if (v === null || typeof v !== "object") return
for (const [k, child] of Object.entries(v)) {
const p = Array.isArray(v) ? path + "[" + k + "]" : path + "." + k
if (/host/i.test(k)) found.push(p)
visit(child, p)
}
}
visit(x, "x")
return found
}'[
"x.services[0].config.db.host",
"x.services[1].config.db.host",
"x.services[2].config.cache.host",
"x.services[3].config.queue.host"
]Each result is a valid fx expression, so you can pass it back in to get the value:
fx services.json 'x.services[3].config.queue.host'mq.internalTIP
To reuse the path finder, define it as a function in .fxrc.js.