Convert JSON to string
Escaped, quoted,
ready to embed.
Serialise any JSON object to an escaped string —
the online equivalent of JSON.stringify().
Includes ready-to-copy code snippets for JavaScript, Python and Java.
JSON to string in every language
The one-liner for each major language and environment.
JavaScript / Node.js
JSON.stringify(obj) — built-in, no import needed. Add a second argument for a replacer and a third for indentation: JSON.stringify(obj, null, 2). To embed in another JSON value, call JSON.stringify(JSON.stringify(obj)).
Python
import json; s = json.dumps(obj). Use json.dumps(obj, indent=2) for pretty output. For embedding: json.dumps(json.dumps(obj)). The ensure_ascii=False flag preserves Unicode characters.
Java
With Jackson: new ObjectMapper().writeValueAsString(obj). With Gson: new Gson().toJson(obj). Both produce a compact JSON string. Use ObjectMapper().writerWithDefaultPrettyPrinter() for indented output.
Other languages
PHP: json_encode($obj). Ruby: obj.to_json or JSON.generate(obj). Go: json.Marshal(obj). C#: JsonSerializer.Serialize(obj) or JsonConvert.SerializeObject(obj).
JSON object to string: when to stringify, and when to Base64-encode
Stringify JSON when the whole document has to travel as one text value: a field inside another JSON payload, a SQL TEXT column, a message-queue body or a test fixture embedded in code. The result is still readable JSON, only escaped. When the value must be safe for URLs, cookies, HTTP headers or tokens, escaping is not enough — encode it with JSON to Base64 instead.
To reverse the process and get the object back, use JSON String to JSON. To shrink the JSON before stringifying it, use the JSON Minifier.
All utilities & encoding tools
