Learn more about JavaScript at https://www.javaguides.net/p/javascript-tutorial-with-examples.html
Usiing JSON.parse() Method
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"}
JSON.parse() Method - Detail Explanation
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
Convert a JavaScript Object to JSON String
References
Continue Learning with Java Guides
Want to take your programming skills to the next level? Explore my practical Udemy courses on Java, Spring Boot, Microservices, Full-Stack Development, Docker, and Generative AI. You can also follow my structured learning roadmaps to become a professional developer.
By Ramesh Fadatare - Udemy Instructor Partner
Comments
Post a Comment
Leave Comment