The first one is valid JSON as well, so if you have a json field in your DB store it as it is.
Sep 29, 2019
4
Level 3
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}}
Level 73
Please or to participate in this conversation.