Skip to content

javascript "library" to convert a list of objects to a nested json output format, depending on the names in the list

Notifications You must be signed in to change notification settings

SauliusTheBlack/formToNestedJson

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 

Repository files navigation

formToNestedJson

javascript "library" to convert a list of objects to a nested json output format, depending on the names in the list

Basic usage

Given a form in html you can serialize it to flat json rather easily using jquery

var json = jQuery("myForm").serializeArray()

This will produce an object of the form

json = [ 
	{"name":"player.firstName", "value":"Saulius"},
	{"name":"player.lastName", "value":"TheBlack"},
];

In the provided example, the form has 2 text inputs with the names player.firstName and player.lastName

if you want to create json that is less flat, and somewhat more verbose you can manually go around and do it everytime yourself. you can also use this library, that will create the following json for you:

json = parseJsonFromForm(jQuery(formId).serializeArray());
json = { 
	"player" : {
		"firstName":"Saulius",
		"lastName":"TheBlack",
	}
};

if there are numbers included in the naming, the parser will convert it to a list:

json = [ 
	{"name":"players.0.firstName", "value":"Saulius"},
	{"name":"players.0.lastName", "value":"TheBlack"},
	{"name":"players.1.firstName", "value":"Gandalf"},
	{"name":"players.1.lastName", "value":"TheWhite"},
];
json = parseJsonFromForm(jQuery(formId).serializeArray());
json = { 
	"players" : [
		{
			"firstName":"Saulius",
			"lastName":"TheBlack",
		},
		{
			"firstName":"Gandalf",
			"lastName":"TheWhite",
		}
	
	]
};

About

javascript "library" to convert a list of objects to a nested json output format, depending on the names in the list

Resources

Stars

Watchers

Forks

Packages

No packages published