close

How to convert FormData (HTML5 object) to JSON

Hello Guys, How are you all? Hope You all Are Fine. Today we are going to learn How to convert FormData (HTML5 object) to JSON in javascript. So Here I am Explain to you all the possible methods here.

Without wasting your time, Let’s start This Article.

How to convert FormData (HTML5 object) to JSON

  1. How to convert FormData (HTML5 object) to JSON

    to convert FormData (HTML5 object) to JSON Abusive one-liner!
    Array.from(fd).reduce((obj, [k, v]) => ({...obj, [k]: v}), {});
    Today I learned firefox has object spread support and array destructuring!

  2. How to convert FormData (HTML5 object) to JSON

    to convert FormData (HTML5 object) to JSON Abusive one-liner!
    Array.from(fd).reduce((obj, [k, v]) => ({...obj, [k]: v}), {});
    Today I learned firefox has object spread support and array destructuring!

Method 1

In 2019, this kind of task became super-easy.

JSON.stringify(Object.fromEntries(formData));

Object.fromEntries: Supported in Chrome 73+, Firefox 63+, Safari 12.1

Method 2

Abusive one-liner!

Array.from(fd).reduce((obj, [k, v]) => ({...obj, [k]: v}), {});

Today I learned firefox has object spread support and array destructuring!

Summery

It’s all About this article. Hope all method helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which method worked for you? Thank You.

Leave a Comment