[jq] Read a nested value from a path with getpath

Created on November 30, 2026

Use getpath to read a value by supplying an array of object keys and array indexes. The path ["users",1,"name"] selects the name of the second user.

printf '%s\n' '{"users":[{"name":"Taro"},{"name":"Hanako"}]}' |
  jq 'getpath(["users",1,"name"])'
"Hanako"

Use strings for object keys and integers for array indexes. The indexes start at 0.

Pass the path in a variable

Use --argjson to pass a JSON path array as a jq variable. The following example reads the first user's name.

printf '%s\n' '{"users":[{"name":"Taro"},{"name":"Hanako"}]}' |
  jq --argjson lookup_path '["users",0,"name"]' 'getpath($lookup_path)'
"Taro"

Reference

jq