function formToJson(form) { if ((typeof form === 'object') && (form.nodeName === 'FORM')) { let entries = new FormData(form).entries(); return JSON.stringify(Object.assign(...Array.from(entries, ([x,y]) => ({[x]:y})))); } return null; }
Пример использования:
console.log(formToJson(document.getElementById('MyForm')));