How to Convert JavaScript Class to JSON in JavaScript?
Last Updated :
04 Sep, 2024
When transmitting or saving class data in a structured manner, converting an instance of a class into a JSON format is also necessary. JSON (JavaScript Object Notation) supplies a method of turning objects into strings. They can be sent across a network or stored in a database, properties of the instance of the class should be taken out and changed into a format of JSON.
These are the approaches to Convert JavaScript Class to JSON in JavaScript:
Using JSON.stringify()
The simplest approach is to use JSON.stringify() which converts objects (classes) to string representation of JSON, however sensible enumeration takes place if and only if default parameters are used due to the fact that by default, it only serializes enumerable properties of a class.
Example: Below is an example demonstrating the use of JSON.stringify( ) to convert the javascript class to JSON in Javascript which converts the input into string format.
JavaScript
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
}
const person = new Person("Pankaj", 20);
const jsonString = JSON.stringify(person);
console.log(jsonString);
Output:
{"name":"Pankaj","age":20}
Custom toJSON()
Method
In case you want to have more control over the mechanism, you may define custom method toJSON() method in your class definition, this method can indicate what properties will be part of the output JSON. This approach allows you to add specific properties, exclude certain fields or even modify the values before conversation.
Example: Below is an example demonstrating use of Custom toJSON( ) Method to convert javascript class to JSON in Javascript.
JavaScript
class Car {
constructor(make, model, year) {
this.make = make;
this.model = model;
this.year = year;
}
toJSON() {
return {
make: this.make,
model: this.model
// you can exclude or include other properties here
};
}
}
const car = new Car("Toyota", "Corolla", 2020);
const jsonString = JSON.stringify(car);
console.log(jsonString);
Output:
{"make":"Toyota","model":"Corolla"}
Conclusion
It is possible to convert a java class instance to JSON format with the help of the JSON.stringify() function of type which is used for Object oriented programming languages since it helps to convert class members to a JSON string, if more customization is needed, implementing a custom toJSON method can target given properties in the object that are included in the conversion.
Similar Reads
How to Convert JSON to Blob in JavaScript ? This article explores how to convert a JavaScript Object Notation (JSON) object into a Blob object in JavaScript. Blobs represent raw data, similar to files, and can be useful for various tasks like downloading or processing JSON data. What is JSON and Blob?JSON (JavaScript Object Notation): A light
2 min read
How to Convert a JavaScript Object to a Form Data in JavaScript? In web development, especially when dealing with forms and AJAX requests, you might find yourself needing to convert a JavaScript object into a FormData object. The FormData interface provides a way to easily construct a set of key/value pairs representing form fields and their values, which can the
4 min read
How to Convert a Map to JSON String in JavaScript ? A Map is a collection of key-value pairs, where each key is unique. In this article, we will see how to convert a Map to a JSON (JavaScript Object Notation) string in JavaScript. However, JSON.stringify() does not directly support Map objects. Table of ContentUsing Object.fromEntries() MethodUsing A
2 min read
How To Convert Base64 to JSON String in JavaScript? There could be situations in web applications, where there is a need to decode the data from Base64 format back into its original JSON format. It generally happens when one has to transmit data over the network where Base64 encoding is well suited for encoding binary data.In this article, we will se
2 min read
How to Convert JSON to string in JavaScript ? In this article, we are going to learn the conversion of JSON to string in JavaScript. Converting JSON to a string in JavaScript means serializing a JavaScript object or data structure represented in JSON format into a textual JSON string for data storage or transmission.Several methods can be used
3 min read
How to Convert Map to JSON in JavaScript ? In JavaScript, when working with data, you might encounter situations where you need to convert a Map object into a JSON format. This can be useful for sending data over the network, storing data in local storage, or interfacing with APIs that expect JSON data. Converting a Map to JSON means convert
3 min read