Reshape JSON with jq 1.7 pipelines
Use jq’s pick(), select() and map() to filter an API response and keep specific nested fields in one output array.
You have an API response with a list of repositories and want the ID, name and owner login for the active ones. Write the projection as a jq filter, then run it on each response you fetch.
Select records and keep three paths
Save this small response as repos.json. Of the two active repositories, one has an owner and the other lacks it.
{
"repos": [
{"id": 12, "name": "docs", "archived": false, "owner": {"login": "ari", "id": 5}},
{"id": 17, "name": "archive", "archived": true, "owner": {"login": "ari"}},
{"id": 19, "name": "cli", "archived": false}
]
}The input holds three repositories, including one archived record. Run this in the same directory with jq 1.7 or later.
jq -c '.repos | map(select(.archived == false) | pick(.id, .name, .owner.login))' repos.jsonThe output is [{"id":12,"name":"docs","owner":{"login":"ari"}},{"id":19,"name":"cli","owner":{"login":null}}]. The owner object stays nested, and the archived repository disappears.
Read the pipeline from left to right. .repos gives map() the array. Inside map(), select() emits each matching record, then pick() keeps just the named paths. map() collects the results into one array. The -c flag prints it on one line.
Pick paths when you want the same shape
jq 1.7 added pick(). Use it to preserve the original field names and nesting. If the destination needs a flat ownerLogin field, use an object constructor and set that field to .owner.login.
A missing owner becomes "owner":{"login":null} because the filter asks for that nested path. If your consumer requires a login, add and (.owner.login != null) to the predicate inside select(). That drops the cli record before the projection. If missing data should stay visible, keep the null and let the consumer handle it. See the jq 1.7 filter reference.
Use the explicit boolean check if the API distinguishes false from an omitted flag. A missing archived field reads as null, so it fails this predicate. Before feeding results into another command, test one record with a missing flag and another with the nested field missing.