Reputation: 167
I have an json object in the form
{
"name" : "ok", "country" : "US","phone" : "900",
"email" : "ok@mail.oc", "time" : "21:00"
}
and I want to convert it into a string which should look like the below example:
"{\n \"name\": \"ok\",\n \"country\": \"US\",\n \"phone\": \"900\",\n \"email\": \"ok@mail.oc\",\n \"time\": \"21:00\",\n \"sendData\": \"300\"\n }"
I tried using JSON.stringify(); but it didn't give the desired output. Is there a easy way to achieve it. Please help me.
Upvotes: 0
Views: 363
Reputation: 167
If you want to achieve the above mentioned pattern you need to pass JSON.stringify
into another JSON.stringify
.
JSON.stringify(JSON.stringify(data));
Upvotes: 1
Reputation: 11464
I believe you want to utilize something like this:
JSON.stringify({ a: 2 }, null, ' ');
The MDN documentation for this is located here. You should be able to tweak this for your desired output.
Upvotes: 0