TailTemplate Build stunning websites faster with our pre-designed Tailwind CSS templates

Quick API design tip

When returning dynamic JSON data from API, if your expectation is a list of ordered data, do not return a key-value object:

{
	"field_1": "value_1",
	"field_2": "value_2",
	"field_x": "value_x",
}

Because JSON objects do not maintain their sequence. The actual result might become


{
	"field_2": "value_2",
	"field_x": "value_x",
	"field_1": "value_1",
}

Solution

The solution is to return an array of objects with fixed attributes names:

[
	{
		"name": "field_1",
		"value": "value_1",	
	},
	{
		"name": "field_2",
		"value": "value_2",	
	},
	{
		"name": "field_x",
		"value": "value_x",	
	}
]

Hope you find this tip useful.