Convert a JavaScript object to JSON

A colleague of mine was recently searching for a plug-in or script that can convert a JavaScript array to JSON. I asked him to check out JSON.stringify() in the json2.js library, which serializes the JavaScript object into JSON text. Here’s a sample if you do not know about this library:

Note: Make sure that you minify this library and then use the .js directly from your application. For demo purposes I am referring to this .js file directly. In production applications, it is not advisable to do so.

<script type="text/javascript" src="">
script type="text/javascript">
arrCars = new Array("Toyota", "Mercedes", "BMW");
var jsonStr = JSON.stringify(arrCars);
JavaScript to JSON

Will you give this article a +1 ? Thanks in advance

About The Author

Suprotim Agarwal
Suprotim Agarwal, ASP.NET Architecture MVP (Microsoft Most Valuable Professional) works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He is the Editor of a Developer Magazine called DNC Magazine. He has also written two EBooks 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook

Follow him on twitter @suprotimagarwal


Elijah Manor said...

As a note, JSON.parse is native in modern browsers & json2.js is helpful for older browsers.

You can see @cowboy's blog post for more details

~/Elijah Manor

Suprotim Agarwal said...

Yep! Thanks for adding that comment Elijah. Here's some more info:

Anonymous said...

What is means of curry. i thing it is a vegetable करी पत्ता

Suprotim Agarwal said...

Curry is a spice mixture of widely varying compositions. My site is called devcurry since it is a mixture of ideas, experiments and thoughts for developers to cook ;)

amrish said...

This is a very good site it contains information for both all kind of users.

Fuzzy said...

the returned string isnt json...

CoursesWeb said...

The link to get the json2.js file seems not work, the page: returns 404 error. But it can be downloaded from:
In modern browsers, the JSON.stringify() works without json2.js .

Anonymous said...

very helpful post.

kapil goutam said...

very helpful

Jayden Bell said...

This is to good, thanks for sharing your knowledge with us. Following links also helped me and develpers.