The impact of data structure on JSON serialization

Amir Ayat
3 min readMay 1, 2023

--

This article will discuss how data structure can affect your RESTful application performance.

Please check the source code on GitHub.

Data Structure
Image by Alain Pham, link

The choice of data structure can have a significant impact on JSON serialization. JSON is a popular data interchange format used for transmitting data between systems. It is based on a key-value pair structure and supports various data types, including strings, numbers, and arrays. When serializing data to JSON, the data structure used to represent the data can impact the resulting JSON output. For example, if the data is represented in a hierarchical tree structure, such as a nested dictionary, the resulting JSON output may contain more nesting and be more verbose compared to a flat structure. Additionally, the choice of data structure can affect the efficiency of JSON serialization and parsing, with some structures allowing for faster processing and smaller output sizes. Overall, the choice of data structure can impact the readability, efficiency, and overall performance of the application.

Project definition

To investigate the impact of data structure on JSON serialization, I have implemented a simple RESTful application in Python, Javascript, Golang, and Asp.Net. Hence, we can see the output result of the application in different programming languages. I have chosen Sanic for Python, Express.js for Javascript, and Fiber for Golang. Before getting deep dive into the process, let's see a famous benchmark of these frameworks. Based on this benchmark, for a single database query to Postgresql, we can expect Asp.Net at the top of the list, followed by Fiber, Sanic, and Express.js.

Web Framework Benchmarks
Web Framework Benchmarks, single database(Postgresql) query, ranked based on the number of requests per second [link]

Test steps

Each application fetches all records of the database via a GET endpoint. The resource data is the list of world countries, taken from this repository.
1. In the first step, our web apps retrieve the records in this model:

first data model
first data model, containing int, nullable string, array, and nested dictionary

Note that “capital”, “native”, “region” and “subregion” are nullable, “timezones” is an array structure, and “translations” is a nested dictionary.
So the database model for the above record would be like this:

database table model
database table model, containing not null bigint, nullable character, and not null jsonb

2. For the second step, I converted the fields “timezones” and “translations” to text. Therefore, the output result is:

second data model
second data model, containing int and nullable string

And the database model for the above record:

database table model, containing not null bigint, nullable character, and not null text

3. In the final step, I replaced the null values with blank string(“”). The database model would be like this:

database table model, containing not null bigint, not null character, and not null text

Test result

After sending 20k requests to all applications at each step, the number of requests served per second diagram is:

The impact of data structure on web framework JSON serialization

Conclusion

  1. Flattening JSON output will increase RESTful application performance.
  2. Replacing nullable fields with uniform datatype will result in higher JSON serialization performance.
  3. Static-type programming languages have better performance.

--

--