14

How can I take a JSON object and convert it back into the original string format?

Thanks

James moore
  • 1,245
  • 4
  • 13
  • 15

4 Answers4

14

You could use an excelent json2.js library: https://github.com/douglascrockford/JSON-js

Check out the JSON.stringify method.

Edit:

It seems like the librabry moved to github repo now: https://github.com/douglascrockford/JSON-js

Piotr Rochala
  • 7,748
  • 2
  • 33
  • 54
5

Use the stringify method from Douglas Crockford's JSON object:

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

This will show your object in an alert box:

alert(JSON.stringify(myObject));
Silkster
  • 2,190
  • 15
  • 28
1

There are a few jQuery plugins to do this. jQuery-json is one of them, which I've tried successfully.

Bruno
  • 119,590
  • 31
  • 270
  • 376
1

when it's an object, it's a javascript object, then it gets encoded as a string, a string that is a javascript syntax that expresses the object json-encoded. (JSON: JavaScript Object Notation).

However, these are the jquery built in function to parse json is jQuery.getJSON (retrieves the object out of the string).

to do things the other way around check this: Serializing to JSON in jQuery

Community
  • 1
  • 1
aularon
  • 11,042
  • 3
  • 36
  • 41