DEV Community

Kazutora Hattori
Kazutora Hattori

Posted on

Explaining JSON.stringify() - The basics of converting objects to strings

Introduction

While developing React, I had the opportunity to use key={JSON.stringify(filters)} in a component, so I'd like to summarize it here.

What is JSON.stringify()?

JSON.stringify() is a JavaScript function that converts objects and arrays to strings.

Basic Usage

const user = { name: "calros", age: 20 };
const json = JSON.stringify(user);

console.log(json);
// → '{"name":"calros","age":20}'
Enter fullscreen mode Exit fullscreen mode

The object is converted to a JSON-formatted string.

Summary

JSON.stringify() is a function for safely handling objects as strings.
In JavaScript, it's often not possible to store or send data as is. Converting data to a string using JSON.stringify() can be useful in a variety of situations.

Top comments (0)