Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

These jq errors usually mean the filter is applying an operation to a value of the wrong type. Use .[] to iterate an array or object—not a number or string—and make both operands the same intended type before using +. Check the input shape first, then choose whether to branch on its type, normalize it, or convert values explicitly.

Why jq says it cannot iterate over a number or string

jq values have types: numbers, strings, booleans, arrays, objects, and null. The iterator .[] is for arrays and objects. If a filter applies it to a scalar such as 7 or "news", jq cannot iterate that value. See the jq development manual for jq’s value model and array construction.

This commonly happens when a field’s JSON shape is different from what the filter assumes. A field expected to contain an array may instead contain one number or string. Inspect the actual input and its type before deciding how to process it.

Choose a fix that matches the input shape

Branch when scalar and collection values need different handling

If the field may legitimately be either a scalar or an array, test its type and handle each case deliberately. For example, an array can be traversed with .[], while a scalar should be processed directly. This preserves the distinction in the input instead of silently changing its meaning.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Normalize only when the filter should treat both cases as a collection

If downstream operations should always receive a collection, explicitly convert the scalar case into a one-element array before iterating. Apply that normalization only when treating one value as one collection item is correct for the data.

Use optional indexing for acceptable missing or non-object fields

Optional indexing, such as .foo?, can prevent an indexing operation from failing when a field is missing or its parent is not an object. It does not turn a number or string into an array, so it is not a general fix for applying .[] to a scalar. The jq 1.6 manual source documents the optional ? form.

Why adding a string and number fails

jq’s + operator depends on operand type: it adds numbers, concatenates arrays, joins strings, and merges objects. It does not automatically convert a number to text or text to a number. Consequently, adding a string and a number is a type mismatch. The jq 1.3 manual documents these type-dependent behaviors.

Decide what the result is supposed to mean before converting anything. For text output, use tostring; for arithmetic, use tonumber only when the input is known to contain valid numeric text. Converting an identifier to a number, for example, can change its meaning if leading zeroes are significant.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Join numeric IDs as text

When a list of numeric IDs needs to become one delimiter-separated string, collect the IDs, convert each to a string, then join them:

[.topics[].id | tostring] | join(";")

The brackets collect the generated strings into an array, and join(";") combines them with semicolons. This pattern addresses the common mismatch between numeric IDs and the string values that join expects. A worked example appears in the DZone explanation of these jq errors.

Prevent the same errors in changing JSON

  • Check the field’s type and shape before using .[].
  • If both scalar and array inputs are valid, choose explicitly between branching and normalizing; do not assume they mean the same thing.
  • Use optional indexing only for cases where a missing or non-object field is acceptable.
  • Before using +, make sure both operands have the intended compatible type.
  • Convert numeric IDs with tostring when producing text; convert text with tonumber only when it is meant to be numeric and contains valid numeric text.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.