convert formdata to json javascript code example

Example 1: FormData to json

var object = {};
formData.forEach(function(value, key){
    object[key] = value;
});
var json = JSON.stringify(object);

// Or ES6 arrow function
var object = {};
formData.forEach((value, key) => object[key] = value);
var json = JSON.stringify(object);

Example 2: convert data into json format in javascript

Use the JavaScript function JSON.parse() to convert text into a JavaScript object:
var obj = JSON.parse('{ "name":"John", "age":30, "city":"New York"}');

Example 3: javascript get form data as json

function handleSubmit(event) {
  event.preventDefault();

  const data = new FormData(event.target);

+ const value = Object.fromEntries(data.entries());

  console.log({ value });
  }

  const form = document.querySelector('form');
  form.addEventListener('submit', handleSubmit);