Form to json
A lightweight module (without Jquery) for serializing html forms into a JSON object. In addition, it supports serialization of input fields with files in base64.
npm i forms_to_json -S
Serialize Form
import FormToJSON from 'forms_to_json';
const $form = document.querySelector('form');
const json = new FormToJSON($form).parse();
console.log(json);
/*
{
"author": "12334",
"year": "1900",
"isGoldCollection": "yes",
"type": "v2",
"hero": [
"spider-man",
"batman"
]
}
*/
Serialize Form with Files
Files converting to base64.
Example serialize HTML form with files.
import FormToJSON from 'forms_to_json';
const $form = document.querySelector('form');
new FormToJSON($form).parseWithFiles().then((json) => {
console.log(json);
/*
{
"author": "War and Peace",
"files": [
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABEwAAAJkCAYAAAALLrTbAA...."
"data:image/png;base64,QLbZMHkY7t0kaPHi3z5s2T0qVLO2wr07p1a8mUKZO21mNf...."
]
}
*/
}).catch(err => {
console.log(err);
});