jq Filters and Transformations/JSON-encode raw text

Turn raw text into a valid JSON string.

Section: Encoding and Recursion

JSON-encode raw text

bash
bash
printf 'hello
world
' | jq -Rs '.'
Explanation

Excellent for embedding arbitrary text into JSON payloads.

Learn the surrounding workflow

Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.

Related commands

Same sheet · prioritizing Encoding and Recursion
HTML-escape a string
Escape special HTML characters.
OpenIn sheetbashsame section
Shell-escape a string
Produce a shell-safe quoted string.
OpenIn sheetbashsame section
Find all keys recursively
Emit every key in nested objects.
OpenIn sheetbashsame section
Find all numbers recursively
Emit every number in a document.
OpenIn sheetbashsame section
Logical AND
Filter items that satisfy two conditions.
OpenIn sheetbash1 tag match
Test regex match
Return true if a string matches a pattern.
OpenIn sheetbash1 tag match