Using Constants to reduce the size of a JSON file

POC by Marin - last mod: 20080318.

Following an article from Vinch, here is a small explanation about JSON Constants

The properties of a JSON are replaced by smaller constants to reduce the weight of a JSON (44% of the original file in this case)


Original JSON

[
	{
		"region" : "Asia",
		"country" : "Thailand",
		"site_account_number" : "039613",
		"centre_status" : "Recruiting",
		"pi_last_name" : "Thongprasert",
		"pi_first_name" : "Sumitra",
		"pi_site_institution_name" : "Maharajnakorn Chiangmai Hospital",
		"primary_investigatordepartment_name" : "- Division of Oncology",
		"primary_investigatorstreet_address" : "110 Intawaroros Road",
		"primary_investigatortowncity" : "Chiang Mai",
		...
		"status" : "active"
	},
	...
]

Modified JSON

[
	{r:"Asia",c:"Thailand",san:"039613",cs:"Recruiting",pln:"Thongprasert",pfn:"Sumitra",psin:"Maharajnakorn Chiangmai Hospital",pin:"- Division of Oncology",pia:"110 Intawaroros Road",pi:"Chiang Mai",...s:"active"},...
]

Constants

All the repeated properties are stored in another JavaScript variable to be sure they don't interfere with other variables of the page.

var GSK_CONSTANTS={
	r:"region",
	c:"country",
	san:"site_account_number",
	cs:"centre_status",
	pln:"pi_last_name",
	pfn:"pi_first_name",
	psin:"pi_site_institution_name",
	pin:"primary_investigatordepartment_name",
	pia:"primary_investigatorstreet_address",
	pi:"primary_investigatortowncity",
	...
	s:"status"
};

Remarks:

Of course the property names can be shorter. The constants can be stored in another file to keep the JSON itself appart.

You could also use a JS Minifier and even GZip your files when browsers support it.

Keep in mind that cache issues could appear on newer version of your JSON.


Demo Constants in JSON

Enter A Center Id