📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
1. Overview
For the best learning experience, I highly recommended that you open a console (which, in Chrome and Firefox, can be done by pressing Ctrl+Shift+I), navigate to the "console" tab, copy-and-paste each JavaScript code example from this guide, and run it by pressing the Enter/Return key.
2. JSON Methods
- JSON.parse() - Parses a JSON string and returns a JavaScript object
- JSON.stringify() - Convert a JavaScript object to a JSON string
2.1. JSON.parse()
Syntax
JSON.parse(text[, reviver])
Parameter Values
- text - This is a required field. A string is written in JSON format
- reviver function - This parameter an optional. A function used to transform the result.
JSON.parse() Method Examples
var text = JSON.parse( '{ "firstName" : "Ramesh", "lastName" : "Fadatare", "emailId" : "ramesh@gmail.com", "age" : "29" }');
console.log(text);
{firstName: "Ramesh", lastName: "Fadatare", emailId: "ramesh@gmail.com", age: "29"}
/*replace the value of "city" to upper case:*/
var text = '{ "name":"John", "age":"39", "city":"New York"}';
var obj = JSON.parse(text, function (key, value) {
if (key == "city") {
return value.toUpperCase();
} else {
return value;
}
});
console.log(text);
{ "name":"John", "age":"39", "city":"New York"}
JSON.parse('{}'); // {}
JSON.parse('true'); // true
JSON.parse('"foo"'); // "foo"
JSON.parse('[1, 5, "false"]'); // [1, 5, "false"]
JSON.parse('null'); // null
2.2 JSON.stringify() Method
Syntax
JSON.stringify(value[, replacer[, space]])
Parameters
- value - This is a parameter is required. The value to convert to a JSON string.
- replacer - This parameter is optional. Either a function or an array used to transform the result. The replacer is called for each item.
- space - This parameter is optional. A String or Number object that's used to insert white space into the output JSON string for readability purposes.
JSON.stringify() Method Examples
var user = {
firstName : 'Ramesh',
lastName : 'Fadatare',
emailId : 'ramesh@gmail.com',
age : 29
}
JSON.stringify(user)
console.log(JSON.stringify(user));
{"firstName":"Ramesh","lastName":"Fadatare","emailId":"ramesh@gmail.com","age":29}
function replacer(key, value) {
// Filtering out properties
if (typeof value === 'string') {
return undefined;
}
return value;
}
var foo = {foundation: 'Mozilla', model: 'box', week: 45, transport: 'car', month: 7};
JSON.stringify(foo, replacer);
"{"week":45,"month":7}"
var foo = {foundation: 'Mozilla', model: 'box', week: 45, transport: 'car', month: 7};
JSON.stringify(foo, ['week', 'month']);
"{"week":45,"month":7}"
JSON.stringify({ a: 2 }, null, ' ');
"{
"a": 2
}"
JSON.stringify({ uno: 1, dos: 2 }, null, '\t');
"{
"uno": 1,
"dos": 2
}
3. References
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
Comments
Post a Comment
Leave Comment