Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

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

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

How to convert FormData (HTML5 object) to JSON

var object = {};
formData.forEach(function(value, key){
    object[key] = value;
});
var json = JSON.stringify(object);
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 :: double question mark javascript 
Javascript :: package json add git repo 
Javascript :: Using webpack 5. Reason: future.webpack5 option enabled https://nextjs.org/docs/messages/webpack5 
Javascript :: split a message 
Javascript :: s3 list objects in folder node js 
Javascript :: create array javascript numbers 
Javascript :: rounding off in javascript 
Javascript :: remove milliseconds from datetime js 
Javascript :: js promis with ajax 
Javascript :: getkey by value js 
Javascript :: How to make the width of a react native element adjust according to the contents 
Javascript :: unhandledpromiserejectionwarning: mongooseerror: the `uri` parameter to `openuri()` must be a string, got "undefined". 
Javascript :: js save files 
Javascript :: nodemailer custom font 
Javascript :: javascript remove last character in a string 
Javascript :: create angular app with routing and scss 
Javascript :: js window width change 
Javascript :: string split javascript newline 
Javascript :: javascript convert string to number or integer 
Javascript :: scroll by javascript 
Javascript :: convert class object to json node js 
Javascript :: reactjs limit text display 20 200 characters 
Javascript :: javascript get random integer in given range 
Javascript :: dynamically adding marker react native mapbox 
Javascript :: window vue remove event listener 
Javascript :: js 2d array to object 
Javascript :: javascript animate elements 
Javascript :: /on in jquery 
Javascript :: javascript length 
Javascript :: splidejs autoscroll pauseOnHover 
ADD CONTENT
Topic
Content
Source link
Name
5+6 =