Reducing JSON Data Size

Lucas Diogo
3 min readFeb 4, 2023

--

A C# approach to reduce payload containing Coordinate Pairs through Gzip and Douglas Peucker Algorithm

Image by DanceProduction.com

JSON (JavaScript Object Notation) is a lightweight data-interchange format that is widely used to exchange data between client and server. However, when dealing with large amounts of data, the size of the JSON file can become a major concern, especially when the data includes a lot of coordinate pairs.

In this article, we’ll explore two methods to reduce the size of a JSON file containing coordinate pairs: Gzip compression and the Douglas Peucker algorithm.

Gzip Compression

Gzip is a popular and widely used data compression method that reduces the size of a file by compressing it using a combination of algorithms. To use Gzip, you need to compress the JSON file on the server side, and then decompress it on the client side. This can be done by using a server-side script or library, such as PHP or Node.js. Once the file has been compressed, you can send it over the network and decompress it on the client side.

Douglas Peucker Algorithm

The Douglas Peucker algorithm is a popular algorithm for reducing the number of points in a curve or polyline. The algorithm works by finding the most significant points in a curve and discarding the rest. This can be useful for reducing the size of a JSON file that contains coordinate pairs, as it can significantly reduce the number of points without sacrificing accuracy.

Douglas-Peucker Algorithm By Wikimedia Commons

Let’s see a C# example:

C# code comparison

Using a set of 1000 coordinate pairs, we can see that compression by itself delivers a very good result, however, both methods combined deliver an even better result.

Results

In conclusion, reducing the size of a JSON file containing coordinate pairs can be done through the use of Gzip compression and the Douglas Peucker algorithm. Both methods can significantly reduce the size of the file, making it easier and faster to transfer data over the network. Whether you’re working with large amounts of data or simply want to improve the performance of your application, these methods are worth considering.

That’s All Folks!

--

--

Lucas Diogo

Developer .NET | Data Science Enthusiast | Writer on Medium in his spare time