Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

json.stringify vs json.parse

/* 
JSON.stringify() takes a JavaScript object and then transforms it into a JSON string.
JSON.parse() takes a JSON string and then transforms it into a JavaScript object.
*/
const myObject = {
  dog: "dog",
  cat: "cat",
  koala: "koala",
  count: 3
};

console.log(JSON.stringify(myObject));
// result: {"dog":"dog","cat":"cat","koala":"koala","count":3}

console.log(JSON.parse(JSON.stringify(myObject)));
// result: Object {dog: "dog", cat: "cat", koala: "koala", count: 3}
Comment

JSON.parse() vs JSON.stringify()


//How To Use JSON.parse() and JSON.stringify()


//JSON.parse() takes a JSON string and transforms it into a JavaScript object.

let userStr = '{"name":"Sammy","email":"sammy@example.com","plan":"Pro"}';

let userObj = JSON.parse(userStr);

console.log(userObj);


/* JSON.parse() can take a function as a second argument that can transform the object values before they are returned.

Here the object’s values are transformed to uppercase in the returned object of the parse method: */

let userStr2 = '{"name":"Sammy","email":"sammy@example.com","plan":"Pro"}';

let userObj2 = JSON.parse(userStr2, (key, value) => {
  if (typeof value === 'string') {
    return value.toUpperCase();
  }
  return value;
});

console.log(userObj2);
console.log("
");



/* JSON.stringify() takes a JavaScript object and transforms it into a JSON string. */

let userObj3 = {
  name: "Sammy",
  email: "sammy@example.com",
  plan: "Pro"
};

let userStr3 = JSON.stringify(userObj3);

console.log(userStr3);

Comment

json parse vs json stringify

parse() takes a JSON string and transforms it into a JavaScript object. JSON. stringify() takes a JavaScript object and transforms it into a JSON string.
Comment

PREVIOUS NEXT
Code Example
Javascript :: java.lang.UnsupportedOperationException: JsonObject 
Javascript :: favicon express js 
Javascript :: phoenix routes 
Javascript :: unity overlap box 
Javascript :: Looping arrays with for loop 
Javascript :: how to add multiple videos in html5 with javascript 
Javascript :: join string js with and at the last item 
Javascript :: javascript afficher 
Javascript :: showdown react 
Javascript :: how to loop elements in javascript for of loop 
Javascript :: 35,2 + 29,4 
Javascript :: "npm supertest 
Javascript :: Prism synchronizationContext 
Python :: ignore warnings 
Python :: python suppress warning 
Python :: how to make a resizable pygame window 
Python :: error tokenizing data. c error 
Python :: convert column in pandas to datetime 
Python :: pandas read csv no index 
Python :: install fastapi conda 
Python :: how to get number of cores in python 
Python :: python if main 
Python :: sqlalchemy query bilter by current month 
Python :: bold text variable in python 
Python :: current datetime pandas 
Python :: python datetime string 
Python :: plot image without axes python 
Python :: python matplotlib log scale 
Python :: dotenv error pip python 
Python :: show image in tkinter pillow 
ADD CONTENT
Topic
Content
Source link
Name
9+8 =