jq --arg vs --argjson: keep build ID 001 from turning into 1
DEV Community

jq --arg vs --argjson: keep build ID 001 from turning into 1

The shell hands a release job two strings: the build ID 001 and the dry-run setting false. The manifest needs to keep the first as text and turn the second into a boolean.

Use --arg for the ID and --argjson for the trusted boolean literal. With jq 1.7, jq -n --argjson id 001 '$id' printed 1 on my machine. JSON numbers do not allow leading zeros, so don't rely on another parser accepting that spelling. Even when the command exits successfully, the ID may be wrong.

The opposite mistake is just as easy to miss: feed false through --arg and jq's conditional treats it as true.

Choose a type for each field

Decide the output fields before choosing flags.

  • The ID is a label: losing zeros changes the name of the artifact.
  • A retry limit is a small integer.
  • A dry-run flag controls a decision and must be a boolean.
  • A file list already encoded as JSON is an array.

Here is the smallest pair that shows what the two options do:

build_id=001 dry_run=false
jq -n --arg id "$build_id" --argjson dry "$dry_run" \
  '{build_id: $id, dry_run: $dry}'

In this test it prints a string for the ID and the JSON literal false for the flag.

Keep both shell expansions quoted: without quotes, a value containing spaces can become several arguments. Shell quotes keep an argument together. They don't decide how jq stores it.

Why the string false enters the wrong branch

Imagine a publication step that treats a truthy dry-run flag as β€œwrite a report, don't deploy.” A shell variable containing false becomes a JSON string when you pass it with --arg.

With raw output (jq -r), both values can look like false in the terminal. The string still selects the dry-run branch.

jq -n --arg flag false '$flag | {value: ., kind: type, truthy: (if . then true else false end)}'
jq -n --argjson flag false '$flag | {value: ., kind: type, truthy: (if . then true else false end)}'

In the first result, kind is "string" and truthy is true. In the second, they are "boolean" and false.

This is not Bash's test behavior leaking through. These are jq conditions and their JSON inputs. In jq, the only false values in a condition are false and null.

If the flag is entered by a human, decide which strings the job supports. You could reject anything except true and false rather than parse arbitrary input. That gives the operator a useful error instead of giving a spelling variant its own accidental meaning.

Test the object the next job reads

Use values that expose the mistake. IDs 123 and 001 are different tests: the second reveals whether the job kept the formatting. A test that only uses true misses this bug: both the boolean and the string take the same branch. Test false too. Compare an empty label with null only if the data format permits both.

In the sample manifest, retries are a policy field from zero through ten. I use one fragment to recognize the exact accepted spelling and convert only then. I don't use the same rule for the build ID.

build_id=001 retries=3
jq -n --arg id "$build_id" --arg retries "$retries" '
  if ($retries | test("\\A(0|[1-9]|10)\\z"))
  then {build_id: $id, retries: ($retries | tonumber)}
  else error("retries must be written as an integer from 0 to 10")
  end
'

That is an input contract for this example, not a rule for every integer a shell operator may enter. I deliberately rejected 03, 2.0 and 2e0 even where a parser could identify a numeric value. If the interface ought to accept those spellings, change the contract and its tests together.

The end of the string matters too. With jq 1.7 on my machine, a pattern ending in a dollar sign accepted the digit 1 followed by a newline. The anchors used above require the entire string to match, so a value copied with a newline at the end gets the same clear error as any other invalid input.

Check the file the next job receives

The first example writes to the terminal. Run the producer again in a scratch directory. The final redirection puts the JSON in manifest.json.

build_id=001 dry_run=false
jq -n --arg id "$build_id" --argjson dry "$dry_run" \
  '{build_id: $id, dry_run: $dry}' > manifest.json

Now check that file from the same directory. Reading valid JSON is not enough: "false" and false both parse.

jq -es --arg expected_id 001 '
  length == 1 and (.[0] | type == "object"
  and (.build_id | type == "string")
  and .build_id == $expected_id
  and (.dry_run | type == "boolean")
  and .dry_run == false)
' manifest.json

The sample prints true and exits with status 0. An empty file, a second object, a shortened ID, or the string "false" prints false and exits with status 1. Malformed JSON fails before the condition runs.

In production, check the file the producer actually hands off. If the expected dry-run flag varies by job, validate the expected value and compare against that boolean rather than hard-coding false.

If it parses but fails the contract, run jq '{id_type: (.build_id | type), dry_run_type: (.dry_run | type)}' manifest.json on a test fixture. Seeing "string" for the dry-run type points to the producer. Fix the flag there instead of making each consumer guess what the string meant.

AI disclosure: AI drafted this article and made the diagrams. The Bash and jq examples were run with made-up inputs.

Sources: jq 1.7 manual: argument options, conditionals, types and conversion; RFC 8259 Β§6: JSON number syntax

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.