Categories
JavaScript Answers

How to convert FormData (HTML5 object) to JSON with JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *