Convert json to a string using jquery

Posted by becomingGuru on Stack Overflow See other posts from Stack Overflow or by becomingGuru
Published on 2010-03-16T10:56:19Z Indexed on 2010/03/16 11:06 UTC
Read the original article Hit count: 353

Filed under:
|
|

I have a nested json. I want to post it as a form input value.

But, seems like jquery puts "Object object" string into the value.

It seems easier to pass around the string and convert into the native form I need, than dealing with json as I don't need to change anything once it is generated.

What is the simplest way to convert a json

var json = {
     "firstName": "John",
     "lastName": "Smith",
     "age": 25,
     "address": {
     "streetAddress": "21 2nd Street",
     "city": "New York",
     "state": "NY",
     "postalCode": "10021"
     },
     "phoneNumber": [
     { "type": "home", "number": "212 555-1234" },
     { "type": "fax", "number": "646 555-4567" }
     ],
     "newSubscription": false,
     "companyName": null
 };

into its string form?

var json = '{
     "firstName": "John",
     "lastName": "Smith",
     "age": 25,
     "address": {
     "streetAddress": "21 2nd Street",
     "city": "New York",
     "state": "NY",
     "postalCode": "10021"
     },
     "phoneNumber": [
     { "type": "home", "number": "212 555-1234" },
     { "type": "fax", "number": "646 555-4567" }
     ],
     "newSubscription": false,
     "companyName": null
 }'

Following doesn't do what I need:

Json.stringify()

© Stack Overflow or respective owner

Related posts about JSON

Related posts about JavaScript