Is it possible to wrap json in json field like a string?

As Marc B noted, inner quotes must be escaped.

{"requestBody":"{\"field1\":111111,\"field2\":\"someValue\"}"}

Fiddle:

http://jsfiddle.net/cheoc1zj/


JSON-encoded stuff is just a string. If you want to embed json-in-json, then the "inner" json has to be encoded into json itself.

e.g.

$inner = {"foo":"bar"}
$outer = {"container":"{\"foo\":\"bar\"}"}

Now the inner json isn't json anymore. It's just a string that happens to kinda/sorta look like JSON.


It won't be pretty, but if you base64 encode the JSON payload, you can be sure it won't be parsed unexpectedly.

How to base64 encode using Javascript: http://www.webtoolkit.info/javascript-base64.html

{  
    "requestBody": "eyJmaWVsZDEiOiAxMTExMTEsImZpZWxkMiI6ICJzb21lVmFsdWUifQ=="
}

Tags:

Json