Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

I have a json like this:

json1 :

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

How can I wrap it in "requestBody" field into json2 like a string?

json2 :

{  
    "requestBody": json1  
}

Something like this:

{  
    "requestBody": "{"field1": 111111,"field2": "someValue"}"
}
like image 599
Denis Avatar asked Aug 29 '14 14:08

Denis


1 Answers

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.

like image 143
Marc B Avatar answered Oct 06 '22 23:10

Marc B