Skip to content

Extracting Specific Fields from JSON ​

When extracting values from JSON with fx, strings are printed as raw text by default. To get properly quoted JSON output, you can append JSON.stringify as a second argument.

Single Field ​

bash
cat data.json | fx .user.name

Output:

Alice

To get a quoted JSON string instead:

bash
cat data.json | fx .user.name JSON.stringify

Output:

json
"Alice"

Multiple Fields ​

bash
cat data.json | fx '{name: x.user.name, email: x.user.email}'

Output:

json
{
  "name": "Alice",
  "email": "alice@example.com"
}

From an Array of Objects ​

bash
cat users.json | fx 'map(u => u.name)'

Output:

json
[
  "Alice",
  "Bob"
]