Interview Prep/Shell Scripting/Troubleshooting: quoting, expansion and tool-input bugs

`jq -r '.items[].metadata.name'` fails with `jq: error (at <stdin>:1): Cannot iterate over null (null)`

IntermediateTroubleshootingTroubleshooting: quoting, expansion and tool-input bugs
Answer

Likely causes → .items does not exist or is null in this input: the API returned an error object, a single object instead of a list, or the field name is different (.Items, .data).

Diagnose →

cmd | jq 'keys'          # what top-level keys exist?
cmd | jq '.items | type'

Fix → Fix the path, or handle absence explicitly: jq -r '.items[]?.metadata.name' or (.items // [])[]. If an error object came back, check the command's exit status before parsing.

Prevent → Check the producer's exit code, then parse; use jq -e when a missing value should fail the script.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Troubleshooting: quoting, expansion and tool-input bugs