Exploring REST API Responses With curl and fx
Raw curl output is one long line of JSON. Pipe it into fx to browse it interactively, pretty print it, or pull out the fields you need with JavaScript. Install fx and try the commands below against https://fx.wtf/example.json.
View a JSON Response From curl in the Terminal
Pipe the response into fx with no arguments to open the interactive viewer:
curl -s https://fx.wtf/example.json | fxUse the arrow keys or vim keys to move, . to run a query, @ to fuzzy-search paths, / to search values by regex (n/N for next/previous), and q to quit. Press ? for the full list of key bindings.
To see the HTTP status line and response headers above the body, add -i:
curl -si https://fx.wtf/example.json | fxWARNING
Headers are only understood by the interactive viewer. With a JS argument, curl -si ... | fx .stats fails with Unexpected character 'H' on line 1. Drop -i when processing.
Pretty Print curl JSON Output
Pass . to print formatted JSON without opening the viewer. An API that returns minified JSON:
{"page":1,"total_pages":3,"data":[{"id":1,"name":"Alice","plan":"pro"},{"id":2,"name":"Bob","plan":"free"}]}curl -s 'https://api.example.com/users?page=1' | fx .Output:
{
"page": 1,
"total_pages": 3,
"data": [
{ "id": 1, "name": "Alice", "plan": "pro" },
{ "id": 2, "name": "Bob", "plan": "free" }
]
}Redirect to save the formatted response: curl -s URL | fx . > response.json.
Extract a Field From an API Response
Start the argument with . to access a property:
curl -s https://fx.wtf/example.json | fx .statsOutput:
{
"totalUsers": 2,
"openIssues": 1,
"closedIssues": 1,
"lastUpdated": "2025-05-08T07:00:00Z"
}Strings are printed without quotes, so the result is ready for shell variables:
curl -s https://fx.wtf/example.json | fx '.users[0].name'AliceExtract a List of Fields From a JSON Array
Use @ to map over an array and ? to filter it. Each argument receives the result of the previous one:
curl -s https://fx.wtf/example.json | fx .users '@{id: x.id, name: x.name, location: x.profile.location}'Output:
[
{ "id": 1, "name": "Alice", "location": "Zurich" },
{ "id": 2, "name": "Bob", "location": "Geneva" },
{ "id": 3, "name": "Charlie", "location": "Zurich" }
]Titles of open issues:
curl -s https://fx.wtf/example.json | fx .issues '?.state == "open"' '@.title'[
"Login page error"
].[] flattens nested arrays; combine it with uniq to list every distinct role:
curl -s https://fx.wtf/example.json | fx '.users[].roles' uniq[
"editor",
"admin",
"viewer"
]Merge Paginated API Responses
Fetch each page in a loop and pipe all responses into one fx. fx reads concatenated JSON as a stream; --slurp collects the pages into one array, and .[].data flattens their data arrays:
for page in 1 2 3; do
curl -s "https://api.example.com/users?page=$page"
done | fx --slurp '.[].data'Output:
[
{ "id": 1, "name": "Alice", "plan": "pro" },
{ "id": 2, "name": "Bob", "plan": "free" },
{ "id": 3, "name": "Charlie", "plan": "pro" },
{ "id": 4, "name": "Dana", "plan": "team" },
{ "id": 5, "name": "Eve", "plan": "free" }
]Add more arguments to keep processing the merged array, for example '@.name' or 'groupBy(x => x.plan)'. Without --slurp, each page is processed separately and prints its own result. To get the page count from the API instead of hard-coding it, use seq "$(curl -s 'https://api.example.com/users?page=1' | fx .total_pages)" in place of 1 2 3.
Use IDs From One Response in the Next curl Request
Print an array one item per line with list, then pass each line to xargs:
curl -s https://fx.wtf/example.json | fx .users '?.active' '@.id' list1
3curl -s https://fx.wtf/example.json | fx .users '?.active' '@.id' list \
| xargs -I{} curl -s https://httpbin.org/anything/users/{} \
| fx .urlhttps://httpbin.org/anything/users/1
https://httpbin.org/anything/users/3The follow-up responses arrive as a JSON stream, so the final fx .url runs once per response.
Build a JSON Request Body and POST It With curl
Reshape part of a response with an object literal and send it with -d @-, which makes curl read the body from stdin:
curl -s https://fx.wtf/example.json \
| fx '.users[0]' '{name: x.name, email: x.email, roles: x.roles}' \
| curl -s -X POST -H 'Content-Type: application/json' -d @- https://httpbin.org/post \
| fx .jsonOutput (httpbin echoes the parsed body back under json):
{
"email": "alice@example.com",
"name": "Alice",
"roles": [
"editor",
"admin"
]
}To send one request per record, turn each item into a single line with JSON.stringify and let xargs post it:
curl -s https://fx.wtf/example.json \
| fx .users '@JSON.stringify({id: x.id, active: x.active})' list \
| tr '\n' '\0' \
| xargs -0 -I{} curl -s -X POST -H 'Content-Type: application/json' --data-binary {} https://httpbin.org/post \
| fx .json.id1
2
3tr '\n' '\0' with xargs -0 keeps the double quotes in the JSON intact.
Save Part of an API Response to a File
fx draws the interactive viewer on stderr, so stdout stays free. Redirect it, browse to the value you want, and press P to print it into the file:
curl -s https://fx.wtf/example.json | fx > part.jsonPress p first to preview the selected value. If you already know the path, skip the viewer: curl -s https://fx.wtf/example.json | fx .issues > issues.json.