Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Form To JSON

function onSubmit( form ){
  var data = JSON.stringify( $(form).serializeArray() ); //  <-----------

  console.log( data );
  return false; //don't submit
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<form onsubmit='return onSubmit(this)'>
  <input name='user' placeholder='user'><br>
  <input name='password' type='password' placeholder='password'><br>
  <button type='submit'>Try</button>
</form>
Comment

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);
Comment

json to formdata

function jsonToFormData (json){
    const FORM_DATA = new FormData();
    if(!json) return FORM_DATA;
    try{
        json = JSON.parse(json);
    }catch{}
    if(typeof json != 'object'||Array.isArray(json)) return FORM_DATA;
    for(key in json) {
        let value = json[key];
        if(Array.isArray(value)){
            value.forEach((v)=>{
                FORM_DATA.append(key+"[]", v);
            });
        }
        else
            FORM_DATA.append(key, typeof json[key] == 'object' ? JSON.stringify(json[key]) : json[key]);
    }

    return FORM_DATA;
}
Comment

json to formData

function buildFormData(formData, data, parentKey) {
  if (data && typeof data === 'object' && !(data instanceof Date) && !(data instanceof File)) {
    Object.keys(data).forEach(key => {
      buildFormData(formData, data[key], parentKey ? `${parentKey}[${key}]` : key);
    });
  } else {
    const value = data == null ? '' : data;

    formData.append(parentKey, value);
  }
}

function jsonToFormData(data) {
  const formData = new FormData();
  
  buildFormData(formData, data);
  
  return formData;
}

const my_data = {
  num: 1,
  falseBool: false,
  trueBool: true,
  empty: '',
  und: undefined,
  nullable: null,
  date: new Date(),
  name: 'str',
  another_object: {
    name: 'my_name',
    value: 'whatever'
  },
  array: [
    {
      key1: {
        name: 'key1'
      }
    }
  ]
};

jsonToFormData(my_data)
Comment

html form data to json

var formData= new FormData(form)
JSON.stringify(Object.fromEntries(formData))
Comment

form data to json

function formDataToJson(formData){
    let json = {};
    if(!(formData instanceof FormData)) return json;
    formData.forEach((value,key)=>{
        json[key] = value;
    });
    return json;
}
Comment

formData to Json

`const formDataObject = (formData) => {

for (const key in formData) {
    if (formData[key].startsWith('{') || formData[key].startsWith('[')) {
        try {
            formData[key] = JSON.parse(formData[key]);
            console.log("key is :", key, "form data is :", formData[key]);

        } catch (error) {
            console.log("error :", key);
        }
    }
}

console.log("object", formData)
}`
Comment

PREVIOUS NEXT
Code Example
Javascript :: Pass unknown number of arguments into javascript function 
Javascript :: set cookie in javascript 
Javascript :: Get Input arrays 
Javascript :: what is console in javascript 
Javascript :: image loading in Compose display image 
Javascript :: javascript injection in mongodb 
Javascript :: add a string regex in angular input 
Javascript :: why .env file in node.js is not working to connect mongodb 
Javascript :: node_modules/react-native-paper/lib/module/core/Provider.js 
Javascript :: how to use of socket io on a route in nodejs 
Javascript :: rendering an array inside an array in react 
Javascript :: react js class component 
Javascript :: e.target.id not working react js 
Javascript :: how to call javascript function with parameter in c# 
Javascript :: react native detect platform 
Javascript :: nodemon 
Javascript :: alpinejs examples stackoverflow 
Javascript :: get all data in collection firebase react 
Javascript :: json schema example 
Javascript :: javascript latitude longitude to km 
Javascript :: Material-ui snowflake icon 
Javascript :: react catch error json message 
Javascript :: javascript append to object 
Javascript :: add jquery through consol 
Javascript :: yup test string async 
Javascript :: array length 
Javascript :: javascript strings vs numbers 
Javascript :: javascript modify href attr 
Javascript :: hide console log level in js 
Javascript :: empty string in javascript 
ADD CONTENT
Topic
Content
Source link
Name
2+5 =