Sometimes, we want to convert FormData (HTML5 object) to JSON with JavaScript.
In this article, we’ll look at how to convert FormData (HTML5 object) to JSON with JavaScript.
How to convert FormData (HTML5 object) to JSON with JavaScript?
To convert FormData (HTML5 object) to JSON with JavaScript, we can use the Object.entries method.
For instance, we write
const json = JSON.stringify(Object.fromEntries(formData));
to call Object.fromEntries with formData to convert the form data object to a plain object.
Then we call JSON.stringify to convert the plain object into a JSON string.
The plain object has the keys from the form data keys and the values from the form data values.
Conclusion
To convert FormData (HTML5 object) to JSON with JavaScript, we can use the Object.entries method.