Charrua's avatar

Storing JSON on database as array or as object

Hello, I have a question for storing some JSON values on my invoices database.

On my taxes column on the invoices table I wanted to store e.g. 2 taxes, each one with some info as name, total, percentage and the id of the tax.

Those taxes are created on a Vue component and then rendered with PHP to create a PDF. So both, Vue and PHP will work with the values stored.

The way I'm using now, is sending data from Vue to PHP. Vue data is:

[{"id":1,"name":"TAX1","percentage":21,"total":210000},
{"id":2,"name":"TAX2","percentage":15,"total":150000}]

And on the PHP side I use json_encode($invoice_taxes) to store them on the db.

My question is how would be the best way to store taxes on the database

Option 1 as array (this is the actual way I'm doing it)

[{"id": 1, "name": "IVA", "total": 630, "percentage": 21}, 
{"id": 2, "name": "IRPF", "total": 210, "percentage": 7}]

Option 2 as object

{"0":{"id":1,"name":"IVA","total":630,"percentage":21},
"1":{"id":2,"name":"IRPF","total":210,"percentage":7}}
0 likes
4 replies
Nakov's avatar
Nakov
Best Answer
Level 73

The first one is valid JSON as well, so if you have a json field in your DB store it as it is.

Tray2's avatar

I'm a bit allergic of storing json in the database since it kind of defeats the purpose of a database. I haven't found a convincing reason to store anything as json. However that is my opinion, you do as you see fit.

yazeedayyash's avatar

i used to store it like this

[{"id":1,"name":"TAX1","percentage":21,"total":210000}, {"id":2,"name":"TAX2","percentage":15,"total":150000}]

i think its easier to handle when you retrieve it

Charrua's avatar

Great, thank you for your help, I'll keep storing them like I used to.

Please or to participate in this conversation.