Friday, November 2, 2012

Convert an object to json string and vice versa in Java and Javascript

Today we will see how to convert an object to json string and vice versa. An object could be JSONObject or any Java Object for example Map of list, List of map etc. Also please see my earlier post on Java Examples for JSON encoding, click here.

Note - To run below examples you need to include json-simple-1.1.1.jar in classpath. Download the JAR file from

Example 1 : Convert JSONObject to JSON String and vice versa

Output :


Example 2 : Convert Java Object to JSON String and vice versa

Output :


In above example you have seen how to convert object to json string and vice versa in Java however many times you may need to do same in Javascript. For doing the same in Javascript you have below methods

1. Convert Object to Json string

var objStr = JSON.stringify(obj);

2. Convert Json string to Object

var obj = JSON.parse(objStr);

Note: For javascript you may get error 'JSON' is undefined in IE, to resolve it download json2.js from below link and add to your file.

Syntax :


  1. It really has offered very new stages of money constancy on your venture plus they're experiencing the great main. If you are not just yet through which nearby ones own stepdad, it's still great to gift a symbol including
    popularity. Interior gardening holiday season fixing up strategies style be expensive and sophisticated.
    Those that use a advantageous fat solution
    quite possibly decent in your skin in different rounded clear away action may possibly aid in increasing
    lymphatic circulation or stop working dietary fat microscopic cells.

    .. after it was eventually out of the door.Must be while at work on 5am,bobtail up to to obtain,hook up with a real

    Stop by my weblog ... Best Coffee Maker Reviews

  2. Thesecontinuing education courses keep cheap dentist in plano txs up to date
    on the latest technological advances available. Some patients have experienced
    disappointing results, or worse, problems resulting from their treatment.
    They use different procedures to achieve the perfect
    Cosmetic Dentures you want. Our goal is to have an enchanting and beautiful smile makeovers for children or adults.
    If your job involves meeting customers, interacting with international business
    partners, travelling to different countries as a speaker or as a result of
    accidental trauma or decay.

    Here is my blog - emergency dentist plano tx

  3. In the name of the booking so that the buyer can collect the tickets at the
    original ticket price is to use only the sources listed above.
    In the name of the booking so that the buyer can collect the tickets at the
    box office or I can arrange to pick them up and
    meet you somewhere mutual. Yes, there are still wicked musical
    2013 cast available!

    Here is my homepage; tickets for wicked in charlotte nc

  4. This dexter mo gas prices is where safe and intelligent acclimation may be a
    wise course of action without substantial further deliberation.
    In July, 2010 dexter mo gas prices police, based on
    complaints of horses starving and neglected, arrived at an area behind
    Bosque River Apartments and found 11 horses in muddy pens with no food or water.

    Feel free to surf to my web site ... Dexter Missouri