7

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()
2

1 Answer 1

12

jQuery doesn't have a method for JSON stringifying native objects. You will need json2.js which will provide the JSON.stringify() method to browsers that don't already support it.

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

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.