Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

json-form-data

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-form-data

A library to convert javascript objects into form data.

  • 1.5.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
8.6K
decreased by-3.79%
Maintainers
1
Weekly downloads
 
Created
Source

json-form-data

A library to convert JavasScript objects into form data.

Build Status BrowserStack Status

Features

  • Supports CommonJS and AMD module loaders
  • Converts nested objects and arrays
  • Compatible with legacy web browsers
  • Supports all primitive data types
  • Supports File and FileList data types
  • Skips null and undefined values
  • Custom value mappings
  • Good unit test coverage

Overview

This library converts a JSON object into form data, allowing files and primitive data to be sent to a server in a single HTTP request.

Single Page Web Applications (SPA's) primarily use JSON formatted payloads. This causes problems when you need to send a file along with additional data, as files cannot be sent to a server in a JSON formatted payload.

This library addresses the limitations of similar libraries by allowing conversion of deeply nested JSON objects, better unit test coverage and exclusion of null and undefined values from the resulting form data.

A custom mapping function allows JSON values to be transformed before they are appended to form data. You can use this to provide your server side with the values it expects.

Usage

Input as JSON

var testObject = {
    prop1: 'test',
    prop2: 2,
    prop3: null,
    prop4: undefined,
    prop5: true,
    prop6: false,
    prop7: new File(['file content'], 'my_file.txt'),
    prop8: {
        prop1: 'test',
        prop2: 2,
        prop3: null,
        prop4: undefined,
        prop5: true,
        prop6: false,
        prop7: [
            'test', 
            2, 
            null, 
            undefined, 
            true, 
            false
        ]
    }
};

var options = {
    showLeafArrayIndexes: true,
    includeNullValues: false,
    mapping: function(value) {
        if (typeof value === 'boolean') {
            return +value ? '1': '0';
        }
        return value;
    }
};

var formData = window.jsonToFormData(testObject, options);

Output as multipart/formdata

prop1
test

prop2
2

prop5
1

prop6
0

prop7
Content-Disposition: form-data; name="My File"; filename="my_file.txt"
Content-Type: text/plain

prop8[prop1]
test

prop8[prop2]
2

prop8[prop5]
1

prop8[prop6]
0

prop8[prop7][0]
test

prop8[prop7][1]
2

prop8[prop7][2]
1

prop8[prop7][3]
0

Options

OptionDefaultDescription
showLeafArrayIndexestrueShows indexes for items in array leaf nodes.
includeNullValuesfalseIncludes null values in the form data.
mappingx => yOverrides the default value mappings true => '1' and false => '0'.

Browser Support

IE / Edge
IE / Edge
Firefox
Firefox
Chrome
Chrome
Safari
Safari
IE10, IE11, Edgelatestlatestlatest

Contributors

Sponsors

BrowserStack Logo

We use BrowserStack to automate our regression tests to ensure compatibility with supported browsers.

Keywords

FAQs

Package last updated on 21 Jul 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc