Please check the source code on GitHub.
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.
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:
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:
2. For the second step, I converted the fields “timezones” and “translations” to text. Therefore, the output result is:
And the database model for the above record:
3. In the final step, I replaced the null values with blank string(“”). The database model would be like this:
Test result
After sending 20k requests to all applications at each step, the number of requests served per second diagram is:
Conclusion
- Flattening JSON output will increase RESTful application performance.
- Replacing nullable fields with uniform datatype will result in higher JSON serialization performance.
- Static-type programming languages have better performance.