JSON Escape & Unescape · Tutorial

How to store a JSON object inside a JSON string field

6 min readintermediateUpdated

Some systems expect a JSON object to be sent as a string. Message queues, webhook "payload" fields, analytics events and some settings files all do this. Pasting the object directly breaks the outer document, because its quotes end the string early. This tutorial shows how to escape the object correctly, wrap it as a string value, and check that the receiver can read it back.

What you need

  • The JSON Escape tool
  • The JSON object you want to embed (the Try it button loads a small order)
  • The outer JSON document where the string will go

Steps

  1. Paste the inner JSON

    Paste the object you need to embed into the left box. The sample is a small order with an orderId, an items array and a note. The right box immediately shows the same text with every double quote turned into \" and every line break turned into \n.

  2. Wrap it in double quotes

    Tick Wrap in double quotes. The result now starts and ends with ", making it one complete JSON string value. This is the exact text you will place after the field name in the outer document.

  3. Make it compact first (optional)

    If you prefer a shorter string without \n sequences, minify the object first with the JSON Formatter (Minify), then paste the one-line version here. The meaning is the same either way.

    "{\"orderId\":4471,\"items\":[\"MUG-01\",\"TEA-07\"],\"note\":\"Leave at the side gate\"}"
  4. Put it into the outer document

    Copy the result and paste it as the value of the string field. Don't add extra quotes around it, because they are already included.

    {
      "event": "order.created",
      "payload": "{\"orderId\":4471,\"items\":[\"MUG-01\",\"TEA-07\"],\"note\":\"Leave at the side gate\"}"
    }
  5. Check that it reads back correctly

    Press Swap ⇄ (or switch to Unescape and paste the escaped value). It should show your original object, and confirm that the result is valid JSON. In code, the receiver does the same thing by parsing the field a second time, for example JSON.parse(event.payload).

Try it yourself

Opens the JSON Escape & Unescape with this tutorial's sample loaded.

Try it now

Common mistakes

Adding a second pair of quotes
With Wrap in double quotes on, the output already includes the quotes. Writing "payload": ""{...}"" breaks the document.
Escaping twice
Running already-escaped text through the tool again turns \" into \\\". If you see triple backslashes, unescape once and start again.
Forgetting the receiver must parse twice
The receiving code gets a string, not an object. If it reads payload.orderId directly, the value is undefined until the string is parsed.

Next steps

Go back to the JSON Escape & Unescape, or keep going with one of these:

FAQ

Is it better to send a nested object instead?

Usually yes, if you control both sides. A nested object is easier to read and validate. Use a string only when the system requires one.