Desk Clarity

Everyday document & browser help

jq string output has quotes: choose raw output without deleting data

By ON Lab · Sources checked November 8, 2026

Use jq -r '.name' input.json when the selected value is a string and the next program expects its text rather than JSON. Normal jq output includes JSON string quotes and escapes. Raw output emits the string's contents. Deleting every quote with a text filter can also delete quotes that belong to the value.

A historical reader wanted to pass an extracted name into another command without surrounding double quotes. We checked the current jq manual and compared ordinary and raw output locally. The right choice depends on the consumer's format and on whether values may contain embedded quotes, backslashes or newlines.

Compare the two representations

printf '%s
' '{"name":"Example"}' | jq '.name'
printf '%s
' '{"name":"Example"}' | jq -r '.name'

The first prints a JSON string with surrounding quotes. The second prints Example followed by the output newline. Both come from the same value; one is a JSON representation and the other is text for a non-JSON interface. Raw mode is a serialization choice, not a repair for invalid input JSON.

If the next tool parses JSON, keep normal JSON output. Removing its delimiters makes a string value invalid as a JSON document. If the next tool expects text, raw mode usually expresses that boundary more accurately than cleaning up formatted JSON afterward.

Preserve quotes that are part of the value

Our second fixture contained a name with literal quote marks, a backslash and a newline. Normal output escaped those characters within a JSON string. Raw output retained the value's actual quote marks and backslash, and the embedded newline became an actual line break.

Input valueNormal outputRaw output
Simple nameJSON quotes around the nameName text
Name containing quote marksEscaped quotes in JSONOriginal quote characters preserved
Name containing a newlineEscaped newline within JSONA real line break inside the output value
Missing field or nullnullStill null, rather than an empty string guarantee

A command that removes all double quotes cannot distinguish JSON syntax from data. It can silently turn a quoted phrase into a different name. Raw mode lets jq parse and serialize the string instead.

Check the selected type and missing-field policy

jq -r '.name | if type == "string" then . else error("name must be a string") end' input.json

This explicit example rejects a missing, null or non-string name. Use it only if that is the application's intended schema. Some applications allow absent names; others distinguish null from an empty string. Choose that policy deliberately and check jq's exit status before the result is used.

In our fixture, raw mode left numbers formatted as numbers and returned the text null for both a missing field selection and explicit null. The -r switch does not prove that a field exists or has the expected type. The separate default-value guide covers value fallback; that is a different decision from emitting a valid string as text.

Do not confuse raw output with shell quoting

Raw output is not escaped shell source. A value containing spaces should remain one argument if that is what the consumer expects. In a shell script, quote variable expansions at the call boundary and use a consumer's supported way to distinguish options from values. Never turn extracted JSON into an eval command.

Shell command substitution also removes trailing newline characters from captured output. That can matter when the field's content intentionally ends in newlines. If exact bytes are required, a direct pipe or file interface may be more appropriate than storing the result in a shell variable. We did not test a particular downstream application, so verify its input rules rather than assuming raw means universally safe.

Choose a record boundary when strings contain newlines

By default, -r adds a newline after each result. An embedded newline can therefore make one value look like several line-oriented records. The manual documents -j for raw output without the added separator, but joining several results can lose their boundaries entirely.

The current manual also documents --raw-output0, which uses NUL delimiters and rejects a value containing NUL. Check support in your installed version and in the receiving program. A NUL-delimited stream only helps when the consumer understands it; it is not a drop-in change for an arbitrary text command. Our initial comparison tested -r, not every consumer or delimiter mode.

Verify before connecting the next command

  1. Confirm that the filter selects the intended field and value type.
  2. Choose JSON or text according to the consumer's documented interface.
  3. Test a name containing spaces, a quote and a backslash.
  4. Test embedded and trailing newlines if permitted by the input schema.
  5. Check the output's record boundaries and the jq exit status.

Keep the original JSON sample while debugging. A successful pipeline that altered the name is not a successful extraction. Compare the actual resulting value with the input field, not just whether surrounding quotes disappeared.

Checked November 8, 2026 with jq 1.7.1. Ordinary and raw output were tested for strings, embedded characters, missing fields, null and numbers. No downstream service, shell execution of data or original application was exercised.

Sources