[SalesForce] Add JSON string in JSON body

Is there a way I can pass JSON string in the JSON body.

Example, Here is my JSON Body,

{"description":"Some Text","machinename":"add JSON","ipaddress":"192.128.0.0","version":"v3"}

In "machinename", I want to add this other JSON string so that it gets stored in the text field as JSON string ({ "id": "0684D0000004VgeTTE", "success": true, "errors": []}, something like below,

{"description":"Some Text","machinename":"{  "id": "0684D0000004VgeTTE", "success": true,    "errors": []}","ipaddress":"192.128.0.0","version":"v3"}

Every time I try to use snippet above, I believe its breaking because of quotation marks ("). Can I skip quotation mark to pass Json sting in JSON?

Best Answer

I suggest:

String encoded = JSON.serialize('{"id": "0684D0000004VgeTTE", "success": true, "errors": []}');

that generates this string that is a valid JSON value:

"{\"id\": \"0684D0000004VgeTTE\", \"success\": true, \"errors\": []}"

that can be set as the "machinename" value. It keeps the logic in the JSON world.

Related Topic