How to Extract Multiple Fields from JSON with jq
Use {name, email} to keep selected fields in one JSON object. For an array of objects, use map({name, email}). Object construction lets you choose the output shape instead of emitting unrelated values.
Paste the filter into the playground, or run it in a Unix shell as jq 'FILTER' input.json. Each example below includes its own input and expected output.
Keep two fields from an object
Input:
{
"id": 7,
"name": "Ada",
"email": "ada@example.com",
"role": "admin"
}
Filter:
{name, email}
Output:
{
"name": "Ada",
"email": "ada@example.com"
}
Run this example in the jq playground
The shorthand {name, email} means {name: .name, email: .email}. The original object is not changed; the filter produces a new object containing the requested fields.
Extract fields from every array element
Input:
[
{
"id": 7,
"name": "Ada",
"email": "ada@example.com"
},
{
"id": 8,
"name": "Lin",
"email": "lin@example.com"
}
]
Filter:
map({name, email})
Output:
[
{
"name": "Ada",
"email": "ada@example.com"
},
{
"name": "Lin",
"email": "lin@example.com"
}
]
Run this example in the jq playground
This returns one JSON array. Use .[] | {name, email} when you deliberately want a stream of separate objects instead. For an API response with a users array, start with .users | map({name, email}).
Rename fields and extract nested values
Input:
{
"id": 7,
"profile": {
"name": "Ada",
"city": "London"
}
}
Filter:
{userId: .id, name: .profile.name, city: .profile.city}
Output:
{
"userId": 7,
"name": "Ada",
"city": "London"
}
Run this example in the jq playground
Keys on the left define your output names; expressions on the right select input values. This is useful when preparing an API response for a table or another service.
Understand missing fields
Input:
{
"name": "Ada"
}
Filter:
{name, email}
Output:
{
"name": "Ada",
"email": null
}
Run this example in the jq playground
A missing field becomes null in this construction. If you need to omit absent keys, use the next example rather than silently treating missing and explicit null values as identical.
Keep only selected keys that actually exist
Input:
{
"name": "Ada",
"email": null,
"role": "admin"
}
Filter:
with_entries(select(.key == "name" or .key == "email" or .key == "phone"))
Output:
{
"name": "Ada",
"email": null
}
Run this example in the jq playground
This preserves an explicitly null email but does not create a missing phone key. By contrast, .name, .email emits two separate values and does not construct an object.
Related guides
- How to Remove Duplicates from a JSON Array with jq
- How to Remove Keys from JSON with jq
- Get array length with jq
- Handle null values and defaults
- jq cheatsheet
Reference: official jq manual.