[jq] Turn a JSON value into a JSON string with tojson

Created on November 30, 2026

Use tojson to serialize an object, array, or other JSON value as text. This example serializes an object with a name and a score.

printf '%s\n' '{"name":"Taro","score":80}' | jq 'tojson'
"{\"name\":\"Taro\",\"score\":80}"

The result is a string. In jq's standard JSON output, double quotes enclose the string and backslashes escape the quotes inside it.

Store the string in an object field

This example serializes the input and stores the resulting string in payload.

printf '%s\n' '{"name":"Taro","score":80}' | jq '{payload: tojson}'
{
  "payload": "{\"name\":\"Taro\",\"score\":80}"
}

Use fromjson to parse that string back into a JSON value.

Reference

jq