fx JSON viewer and processor

Version 40 · source

Start here

fx <file>
Browse JSON interactively
curl <url> | fx
Browse piped JSON
fx <file> .<key>
Pick one field
fx <file> '.items.map(x => x.id)'
Transform with JS
fx <file> .
Pretty-print
fx <file> .items @.name
Map over an array
fx <file> .items '?.active'
Filter an array
fx --yaml <file>
Read YAML

Concepts

  1. Input file or stdin
  2. Arg 1 .users
  3. Arg 2 @.name
  4. Output JSON or text
Each argument gets the previous result as x

Run and input

fx <file>
Open a file interactively
<cmd> | fx
Open piped JSON interactively
fx <file> <expr>
Apply an expression, print result
<cmd> | fx .
Pretty-print stdin
fx <file> <expr1> <expr2>
Chain expressions
curl -i <url> | fx
Show response headers too
fx --slurp <expr>
Read a stream as one array
fx --strict .
Reject invalid JSON
fx --version
Print version
fx --comp bash
Print shell completion script
Also zsh and fish: source <(fx --comp zsh)

Expressions

.<key>
Select a field
.<key>[0]
Select an array element
.<key>[]
Flatten array items
x => x.<key>
Explicit arrow function
'.map(x => x.<key>)'
Map over an array in JS
'.filter(x => x.<key> > 1)'
Filter an array in JS
@.<key>
Shorthand for map
?.<key>
Shorthand for filter
'({...x, <key>: 1})'
Copy an object with changes
'Object.keys'
Call any JS function by name
'`Hi ${x}`'
Template string output
'x.<key> = 1, x'
Edit, then return the input
'x.id = 12345678901234567890n'
Big integers need an n suffix
Numbers above the JS safe limit are parsed as BigInt.

Built-in functions

len
Length of array, string or object
keys / values
Object keys or values
sort / sortBy(fn)
Sort an array
uniq
Unique values
groupBy(fn)
Group items by a result
map(fn) / filter(fn)
Curried map and filter
flatten
Flatten nested arrays
reverse
Reverse an array
chunk(<n>)
Split into groups of n
walk(fn)
Apply fn to every value, bottom-up
list
Print array as lines for xargs
save
Write edits back to the input file
toBase64 / fromBase64
Encode or decode base64
YAML.stringify
Convert JSON to YAML
YAML.parse
Convert YAML text to JSON

Formats

--yaml
Read YAML
--toml
Read TOML
--xml
Read XML (attributes as "@name")
--html
Read HTML
--edn / --maml
Read EDN or MAML
--raw / -r
Process input line by line as text
-rs
Raw lines as one string array

Interactive: move

Arrows
Move; h j k l also work
g / G
Top / bottom
Space / b
Page down / up
d / u
Half page down / up
J / K
Next / previous sibling
[ / ]
Back / forward in history
@
Fuzzy-search a path and jump
Ctrl+G
Go to reference
q
Quit
:<n>
Jump to line n

Interactive: fold and view

Enter / l
Expand node
Backspace / h
Collapse node
L / H
Expand / collapse recursively
e / E
Expand / collapse all
1-9
Collapse to nth level
z
Toggle string wrap
s
Show sizes or line numbers
?
Show help

Interactive: search and output

.
Open query input
/
Regex search
n / N
Next / previous match
Tab / Shift+Tab
Complete or cycle in query input
Up / Down
Browse query history in input
Esc
Cancel the query
y
Yank (copy) value
p
Preview value
P
Print value to stdout and exit
d d
Delete node
/<re>/
Case-sensitive search
/<re>/i
Explicit case-insensitive search
:w <file>
Write on-screen JSON to a file
:w !<cmd>
Pipe on-screen JSON to a command
Keep the space before !; :w!x forces a write to a file named x.
:wq
Write and quit

Recipes

Pull one column as lines
curl -s <url> | fx .items @.name list
Convert YAML config to JSON
fx config.yaml . > config.jsonkubectl get pod -o yaml | fx --yaml .spec
Rename a field in place
fx <file> 'x.name = x.name.toUpperCase(), x' save
Explore, pick, save
curl -s <url> | fx > part.json# move to a value, press P