Database – Should a SVG be converted to JSON to be stored in the database

databaseparsingstorage

Consider an application where you can create and edit a SVG file (e.g. draw.io).

How should the SVG be stored in the database?

With SVG (image/svg+xml)

Thus no further processing is required before the data is stored. But after the data is retrieved the SVG needs to be parsed so the application can set up event handlers and integrate the SVG with the functionality of the application.

With JSON (application/json)

The SVG data will have to be converted to a JSON object what will be stored in and retrieved from the database. When retrieving the JSON data from the database. The data must be converted back to SVG and while doing that the functionality of the application may be applied to the SVG.


Storing and retrieving the data isn't probably the part to be concerned about because it will take about the same amount of time and space to store or retrieve a SVG or JSON (depending on the differences in attribute/key length). Thus I guess the real performance issues occur at parsing the data. I know parsing JSON is easier and faster then XML, however when storing as SVG parsing of the data happens only once with JSON twice.

An other software engineer in my team told me to use JSON but couldn't substantiate why? Perhaps someone can substantiate why JSON should be chosen?

Best Answer

An other software engineer in my team told me to use JSON but couldn't substantiate why?

Maybe it was a joke? I can't think of any benefit to storing SVG inside JSON. None.

I would store SVG files...in SVG format. Storing them in a file or blob storage system would be preferable to a database IMHO, but a database isn't a terrible option if the file sizes are small.

Related Topic