Want to Level Up Your Coding Skills?
Subscribe to Ram N Java for simple, beginner-friendly tutorials every week!
Subscribe on YouTubeWhat is JSON Serialization?
When sending data from a web browser to a server, the data must be formatted as text. JavaScript objects exist in browser memory, but servers expect structured text strings.
This conversion process is called JSON serialization. JavaScript provides a built-in method called JSON.stringify() that takes a JavaScript object and converts it into a standard JSON string in just one line of code.
How JSON.stringify() Works
- Create your JavaScript object: Define your data with standard key-value pairs (names, ages, cities, arrays, etc.).
- Pass the object to JSON.stringify(): The browser serializes the keys and values into valid JSON format.
- Send or store the text: The resulting string is ready to be sent across an API, stored in local storage, or saved to a database.
Practical Code Example
// 1. Define a JavaScript object
const person = {
name: "Peter",
age: 30,
city: "New York"
};
// 2. Convert object to a JSON string
const myJSON = JSON.stringify(person);
// 3. Output the result
document.getElementById("output").innerHTML = myJSON;
The output produced on your page is a valid JSON string:
{"name":"Peter","age":30,"city":"New York"}
Hello nice blog thanks for posting. Master Java Enterprise Edition (J2EE) with our specialized Java Training in Chennai
ReplyDelete