Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
What is JSON text ?
Next article icon

JavaScript JSON stringify() Method

Last Updated : 14 Mar, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The JSON.stringify() method in JavaScript is used to convert JavaScript objects into a JSON string. This method takes a JavaScript object as input and returns a JSON-formatted string representing that object.

Syntax:

JSON.stringify(value, replacer, space);

Parameters:

  • value: It is the value that is to be converted into a JSON string.
  • replacer: It is an optional parameter. This parameter value can be an altering function or an array used as a selected filter for the stringify. If the value is empty or null then all properties of an object are included in a string.
  • space: It is also an optional parameter. This argument is used to control spacing in the final string generated using the JSON.stringify() function. It can be a number or a string if it is a number then the specified number of spaces are indented to the final string and if it is a string then that string is (up to 10 characters) used for indentation.

Return Value: Returns a string for a given value.

JavaScript JSON stringify() Method Examples

Example 1: Converting JavaScript Object to JSON String

The code demonstrates how to convert a JavaScript object obj into a JSON string using JSON.stringify(). The resulting JSON string represents the properties of the object in a serialized format.

JavaScript
const value = {     Company: "GeeksforGeeks",     Estd: 2009,     location: "Noida" }; const result = JSON.stringify(value); console.log("value of result = " + result); 

Output
value of result = {"Company":"GeeksforGeeks","Estd":2009,"location":"Noida"}   

Example 2: Deep Copying JavaScript Object with JSON.stringify() and JSON.parse()

The code creates an object obj with nested properties. JSON.stringify() converts obj to a JSON string, then JSON.parse() parses it back to an object obj2. Modifying obj2 doesn’t affect obj, illustrating deep copying.

JavaScript
let obj = {     name: "GFG",     add: {         country: "India",         state: {             code: "JS",             topic: "stringify"         }     } }  let obj2 = JSON.parse(JSON.stringify(obj)); obj2.add.state.topic = "stringify json object"; console.log(obj); console.log(obj2); 

Output
{   name: 'GFG',   add: { country: 'India', state: { code: 'JS', topic: 'stringify' } } } {   name: 'GFG',   add: {     country: 'India',     state: { code: 'JS', topic: 'stringify json object' }   } ...  

Example 3: Converting Array to JSON String

The code converts the array value into a JSON string using JSON.stringify(). The resulting string result is logged along with its type. This demonstrates how JSON.stringify() converts JavaScript data types into JSON strings.

JavaScript
let value = ["Logan", 21, "Peter", 24]; let result = JSON.stringify(value); console.log("value of result = " + result); console.log("type of result = " + typeof result); 

Output
value of result = ["Logan",21,"Peter",24] type of result = string   

We have a complete list of Javascript JSON methods, to check those please go through Javascript JSON Complete Reference article.

Supported browsers:

  • Chrome 4.0
  • Firefox 3.5
  • Microsoft Edge 12.0
  • Opera 11.0
  • Internet Explorer 8.0
  • Safari 4.0

Next Article
What is JSON text ?

V

vivekkothari
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • JavaScript-Methods

Similar Reads

  • JSON Tutorial
    JSON (JavaScript Object Notation) is a widely-used, lightweight data format for representing structured data. It is used extensively in APIs, configuration files, and data exchange between servers and clients. JSON (JavaScript Object Notation) and XML (eXtensible Markup Language) are popular formats
    6 min read
  • JSON Introduction
    JSON (JavaScript Object Notation) is a lightweight, text-based data format used for representing structured data. It is one of the most widely used formats for data interchange between clients and servers in web applications. JSON has replaced XML as the preferred data format due to its simplicity,
    5 min read
  • JSON Full Form
    JSON stands for JavaScript Object Notation. It is a popular data interchange format used in many applications and technology stacks. JSON is easy for both humans and machines to read.It is not tied to any specific programming language and can be combined with C++, Java, and Python.It represents data
    1 min read
  • JSON Data Types
    JSON (JavaScript Object Notation) is the most widely used data format for data interchange on the web. JSON is a lightweight text-based, data-interchange format and it is completely language-independent. JSON Data Types JSON supports mainly 6 data types: StringNumberBooleanNullObjectArrayNote: strin
    2 min read
  • JSON Schema
    JSON Schema is a content specification language used for validating the structure of a JSON data.It helps you specify the objects and what values are valid inside the object's properties. JSON schema is useful in offering clear, human-readable, and machine-readable documentation. Structure of a JSON
    2 min read
  • JavaScript JSON
    JSON (JavaScript Object Notation) is a lightweight data format for storing and exchanging data. It is widely used to send data between a server and a client. JSON is simple, language-independent, and easy to understand. JSON stands for JavaScript Object Notation.It is a lightweight, text-based data
    5 min read
  • JavaScript JSON stringify() Method
    The JSON.stringify() method in JavaScript is used to convert JavaScript objects into a JSON string. This method takes a JavaScript object as input and returns a JSON-formatted string representing that object. Syntax: JSON.stringify(value, replacer, space);Parameters: value: It is the value that is t
    3 min read
  • What is JSON text ?
    JSON text refers to a lightweight, human-readable format for structuring data using key-value pairs and arrays. It is widely used for data interchange between systems, making it ideal for APIs, configuration files, and real-time communication. In today’s interconnected digital world, data flows seam
    4 min read
  • Interesting facts about JSON
    JSON or JavaScript Object Notation is a lightweight format for transporting and storing data. JSON is used when data is transferred from a server to a web page. This language is also characterized as weakly typed, prototype-based, dynamic, and multi-paradigm. Here are some interesting facts about JS
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences