1. Object to string

use JSON.stringfy()

JSON.stringify() take javascript Object converted to pass Web String required by the server to send data .
grammar :
JSON.stringify(obj)
parameter : Can be an object , array .
const obj = { id: 0, name: ' Zhang San ', age: 12 } const objToStr =
JSON.stringify(obj) console.log('obj:', obj) // obj: {id: 0, name: ' Zhang San ', age:
12} console.log('objToStr:', objToStr) // objToStr:
{"id":0,"name":" Zhang San ","age":12}
Parameter is array ( object array )
var obj_to_str={ name: "GeeksForGeeks", city: "Noida", contact:2488 };
var myJSON = JSON.stringify(obj_to_str); console.log(myJSON) //
{"name":"GeeksForGeeks","city":"Noida","contact":2488}
use toString Why not?
{}.toString();// report errors ,Unexpected token . ({}).toString();//[object Object]
({a:123}).toString();//[object Object] Object.toString();//"function Object() {
[native code] }" function Person(){ this.name = 'test'; } var person1 = new
Person(); person1.toString();//"[object Object]"
2. json String to object

use parse method
const str = '{"id":0,"name":" Zhang San ","age":12}' const strToObj = JSON.parse(str)
console.log('str:', str) // str: {"id":0,"name":" Zhang San ","age":12}
console.log('strToObj:', strToObj) // strToObj: {id: 0, name: ' Zhang San ', age: 12}

Technology
Daily Recommendation