Skip to content
This repository


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Alternative decoder for form-urlencoded data

tree: 967ababc05

Fetching latest commit…

Cannot retrieve the latest commit at this time


Form2json is a decoder for x-www-form-urlencoded data that supports complex structures. It doesn't munge the parameter names and uses simple dot-notation for nested properties. Square brackets only to denote arrays.


Here are some examples to illustrate the syntax.

Nested Objects

The following string represents a nested object:

The string will be decoded as:

    planet: {
        name: "Mars",
        diameter: 0.532

There are no restrictions on the nesting-level:


    planet: {
        mars: {
            diameter: 0.532,
            mass: 0.11


Arrays can be encoded using square brackets:


    planets: ["Venus", "Earth", "Mars"]

If the index is omitted, form2json will push the values to the array in order of appearance:


    planets: ["Mars", "Venus", "Earth"]

This feature is really useful, as it allows you to re-order the input fields on the client, without having to update the field names. In order to use this feature with arrays containing nested objects, we have to use a slightly different syntax:


    planets: [
        { name: "Mars", mass: 0.11 },
        { name: "Venus, mass: 0.82 },
        { name: "Earth", mass: 1 }

In fact you may use any non-numerical value as index. All items having the same index will be grouped into one object.

Connect Middleware

You can use form2json together with the bodyDecoder middleware:

require('connect/middleware/bodyDecoder').decode['application/x-www-form-urlencoded'] = require('form2json').decode;
Something went wrong with that request. Please try again.