Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

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;
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #json #formdata
ADD COMMENT
Topic
Name
3+4 =