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


10 Responses to "Convert a JavaScript object to JSON"
  1. Elijah Manor said...
    March 10, 2010 at 10:26 AM

    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

  2. Suprotim Agarwal said...
    March 10, 2010 at 11:12 AM

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

  3. Anonymous said...
    March 11, 2010 at 4:58 AM

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

  4. Suprotim Agarwal said...
    March 11, 2010 at 6:15 AM

    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 ;)

  5. amrish said...
    March 12, 2010 at 2:14 AM

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

  6. Fuzzy said...
    August 16, 2011 at 7:52 AM

    the returned string isnt json...

  7. CoursesWeb said...
    April 23, 2012 at 11:20 AM

    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 .

  8. Anonymous said...
    May 21, 2012 at 11:22 PM

    very helpful post.

  9. kapil goutam said...
    May 21, 2012 at 11:23 PM

    very helpful

  10. Jayden Bell said...
    June 20, 2013 at 8:42 AM

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


Copyright © 2009-2016 All Rights Reserved for by Suprotim Agarwal | Terms and Conditions