[jq] Pass JSON from a shell variable with --argjson

Created on November 30, 2026

Use --argjson when a shell variable contains valid JSON and you want jq to retain its type. This example passes a number and an array.

shell_count=3
shell_tags='["linux","jq"]'
printf '%s\n' '{}' | jq \
  --argjson count "$shell_count" \
  --argjson tags "$shell_tags" \
  '.count = $count | .tags = $tags'
{
  "count": 3,
  "tags": [
    "linux",
    "jq"
  ]
}

The two invocations bind $count and $tags inside the filter. The filter sets the corresponding fields using those variables. Use --arg for ordinary text. A JSON string passed through --argjson must include its JSON quotation marks.

Reference

jq