0

I know jQuery automatically turns JSON into string when sending AJAX POST requests. Is it possible to convert JSON to string the same way without sending it via AJAX?

0

2 Answers 2

4

Use the JSON stringifier at JSON.org

http://www.json.org/js.html

BTW I don't think jQuery is converting JSON to a string. I think the conversion of the JSON to a POST message is done internally by the browser as part of the XmlHttpRequest.... but could be wrong... haven't really looked at the code in jQuery for few versions.

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

2 Comments

To do the opposite, i.e. JSON -> object one would use the jQuery.parseJSON() function.
Agreed, this is new in jQuery. I used to use the JSON.org library for parsing too but the jQuery implementation is nice, especially since it will use a browser native implementation if it's available.
0

Most browsers have a native JSON object these days, which includes parse and stringify methods. So just try JSON.stringify({}) and see if you get "{}". You can even pass in parameters to filter out keys or to do pretty-printing, e.g. JSON.stringify({a:1,b:2}, null, 2) puts a newline and 2 spaces in front of each key.

JSON.stringify({a:1,b:2}, null, 2)

gives

"{\n  \"a\": 1,\n  \"b\": 2\n}"

which prints as

{
  "a": 1,
  "b": 2
}

See http://www.javascriptkit.com/jsref/json.shtml for more info.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.