0

I have an json object in the form

{
    "name" : "ok", "country" : "US","phone" : "900",
    "email" : "[email protected]", "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\": \"[email protected]\",\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.

2
  • Which language are you using? Javascript? Commented Mar 15, 2017 at 18:48
  • Im using jquery Commented Mar 15, 2017 at 18:49

2 Answers 2

1

If you want to achieve the above mentioned pattern you need to pass JSON.stringify into another JSON.stringify.

JSON.stringify(JSON.stringify(data));

Sign up to request clarification or add additional context in comments.

Comments

0

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.

2 Comments

hi can you help me with this tried but im only able to get the space and able to get the \
I've tried to help you, but have no idea what you are actually try to accomplish. You will need to add more details to your question. What does that mean you are only able to get the space and the \? What else are you trying to get? How does what you are getting compare to what you want? How are you trying to use this json string?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.