[jq] Avoid the startswith() requires string inputs error

Created on November 30, 2026

startswith expects a string. The following command outputs the first match and then raises an error when it encounters null.

echo '[{"name":"Taro"},{"name":null}]' | jq '.[] | select(.name | startswith("T"))'
{
  "name": "Taro"
}
jq: error (at <stdin>:1): startswith() requires string inputs

Check type before calling startswith. With type == "string" and startswith("T"), jq only tests the prefix for string values.

echo '[{"name":"Taro"},{"name":null},{"name":123},{"name":"Satoshi"}]' | jq '.[] | select(.name | type == "string" and startswith("T"))'
{
  "name": "Taro"
}
jq