Convert a Javascript Object to JSON String

This article demonstrates easy ways to convert a javascript object or an array of objects to JSON String using different methods and example illustrations.

Table of Contents:

JSON.stringify()

Javascript’s stringify() method will convert a javascript value or an object to a JSON string. It can optionally replace values with a replacer function passed in as the second parameter and optionally insert whitespaces to the output of JSON string passed in as the third parameter.

Syntax:-

JSON.stringify(jsValue)
JSON.stringify(jsValue, replacer)
JSON.stringify(jsValue, replacer, space)
  • jsValue: jsValue is the value to be replaced with the JSON string.
  • replacer: replacer is the function to change the behavior of the stringification process, or it can be an array of String and Number that acts as a select list of properties to be included in JSON String. The JSON String will consist of all the object properties if this value is null or not provided.
  • space: space is used to add spaces to the JSON string for readability. This parameter can be a String or a Number.

Convert Javascript Object to JSON String using JSON.stringify() method

Example :-

Convert the object to a JSON String { personFirstName: ‘George’, personLastName: ‘Smith’, dateOfBirth: ‘Nov 14 1984’ , city : ‘Santiago’};

Code:-

let personObject =  { personFirstName : 'George',
                      personLastName  : 'Smith',
                      dateOfBirth     : 'Nov 14 1984' ,
                      city            : 'Santiago'
                    };

var stringJSON = JSON.stringify(personObject)
console.log("JSON String: "+ stringJSON);

Output:-

JSON String: {"personFirstName":"George","personLastName":"Smith","dateOfBirth":"Nov 14 1984","city":"Santiago"}

To beautify the JSON string, use the third parameter to pass whitespaces. Observe the below code.

let personObject =  { personFirstName : 'George',
                      personLastName  : 'Smith',
                      dateOfBirth     : 'Nov 14 1984' ,
                      city            : 'Santiago'
                    };

console.log("Beautified JSON String: " + JSON.stringify(personObject, null, 4) )

Output:-

Beautified JSON String: {
    "personFirstName": "George",
    "personLastName": "Smith",
    "dateOfBirth": "Nov 14 1984",
    "city": "Santiago"
}

Convert Javascript Array of Objects to JSON String using JSON.stringify() method

Example :-

Convert the array of objects to a JSON String [ { voterName: ‘Paul’, eligibility: ‘Yes’ }, { voterName: ‘Eva’, eligibility: ‘No’}, { voterName: ‘Veronica’, eligibility: ‘Yes’ }];

Code:-

var myObjectArray = [
  { voterName: 'Paul', eligibility: 'Yes' },
  { voterName: 'Eva', eligibility: 'No'},
  { voterName: 'Veronica', eligibility: 'Yes' }
];

console.log("Beautified JSON String: " + JSON.stringify(myObjectArray, null, 4) )

Output:-

Beautified JSON String: [
    {
        "voterName": "Paul",
        "eligibility": "Yes"
    },
    {
        "voterName": "Eva",
        "eligibility": "No"
    },
    {
        "voterName": "Veronica",
        "eligibility": "Yes"
    }
]

I hope this article helped you convert a javascript object / array of objects to a JSON string. Good Luck !!!

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top