JavaScript JSON.stringify() with Example

The JavaScript JSON.stringify() is used for converting JavaScript object into a string. It is particularly useful when sending data to a web server.

Following is the code for JSON stringify() method −


 Live Demo

<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
   body {
      font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
   .sample {
      font-size: 18px;
      font-weight: 500;
      color: red;
<div class="sample"></div>
<div style="font-weight: bold;" class="result"></div>
<button class="Btn">CLICK HERE</button>
Click on the above button to convert the above object into a JSON string
   let sampleEle = document.querySelector(".sample");
   let resultEle = document.querySelector(".result");
   let obj = {name: "Rohan",sports: ["Cricket", "Football"],Country: "India",};
   sampleEle.innerHTML ="Name = " +"<br>" +" Country = " +obj.Country +
      " <br>" +"sports = " +obj.sports;
   document.querySelector(".Btn").addEventListener("click", () => {
      resultEle.innerHTML = JSON.stringify(obj);


On clicking the ‘CLICK HERE’ button −

Updated on: 11-May-2020


Kickstart Your Career

Get certified by completing the course

Get Started