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 :: avascript regex email 
Javascript :: iife js arrow function 
Javascript :: javascript json deserialize 
Javascript :: new line javascript 
Javascript :: preview upload image js 
Javascript :: how to count specific letters in string js 
Javascript :: function call ready resize and load 
Javascript :: js check if two arrays contain same values 
Javascript :: redux extension 
Javascript :: multiple case switch javascript 
Javascript :: array js fill 
Javascript :: add on click to div using jquery 
Javascript :: canactivate angular 
Javascript :: jquery convert time to 1day 2 minutes 4 seconds 
Javascript :: how to send a request to a web server javascript 
Javascript :: jquery change button click function 
Javascript :: finddomnode is deprecated in strictmode 
Javascript :: how to find all elements starting with class jquery 
Javascript :: js is undefined or null 
Javascript :: get all image tags javascript 
Javascript :: select elements of an array starting by a certain letter javascript 
Javascript :: use of .json() in javascript 
Javascript :: javascript pre increment and post increment 
Javascript :: socket io emit to socket id 
Javascript :: javascript date object format yyyy mm dd 
Javascript :: javascript date to string format dd mmm yyyy 
Javascript :: reducer in react example 
Javascript :: javascript merge arrays of objects without duplicates 
Javascript :: javascript convert to array 
Javascript :: how to repeat an array of objects n times in javascript 
ADD CONTENT
Topic
Content
Source link
Name
7+1 =