Giter Club home page Giter Club logo

form2json's Introduction

About

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.

Syntax

Here are some examples to illustrate the syntax.

Nested Objects

The following string represents a nested object:

planet.name=Mars&planet.diameter=0.532

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&planet.mars.mass=0.11

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

Arrays

Arrays can be encoded using square brackets:

planets[0]=Venus&planets[1]=Earth&planets[2]=Mars

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

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

planets[]=Mars&planets[]=Venus&planets[]=Earth

{
	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[$2].name=Mars&planets[$2].mass=0.11&planets[$0].name=Venus&planets[$0].mass=0.82&planets[$1].name=Earth&planets[$1].mass=1

{
	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;

form2json's People

Contributors

eirikurn avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.