[jq] Load a text file as a string with --rawfile

Created on November 30, 2026

Use --rawfile to read an entire file into a string variable. This example reads message.txt and assigns its contents to a JSON field.

printf '%s\n' 'Hello "jq"' 'Second line' > message.txt
jq -n --rawfile file_text message.txt '{message: $file_text}'
{
  "message": "Hello \"jq\"\nSecond line\n"
}

-n runs the filter without a JSON input stream. The file data can be ordinary text. Newlines, including the final one, remain part of the value. jq escapes newlines and double quotes when serializing the string as JSON.

Use --slurpfile when you want to parse a file of JSON values into an array.

Reference

jq