1
I have been studying Json recently to use in my projects to replace database in some cases, in my tests I found a doubt. depending on the project I may end up repeating some values in certain items several times during the code for example
[
{
"Empresa" : "Unimed",
"Categoria" : "Hospital",
"Tags" : ["Plano de saude" , "Clinicas diversas", "Venda de serviços" ],
"Endereco" : "Rua Fulano de Tal",
"IconeEmpresa" : "icone.png",
"IconeCategoria": "hospital.png"
},
{
"Empresa" : "Ipiranga",
"Categoria" : "Posto de gasolina",
"Tags" : ["Venda de Produtos", "Venda de serviços" ],
"Endereco" : "Rua Fulano",
"IconeEmpresa" : "icone2.png",
"IconeCategoria": "posto.png"
},
{
"Empresa" : "Beneficente",
"Categoria" : "Hospital",
"Tags" : ["Plano de saude" , "Venda de serviços" ,"Medico Especializado"],
"Endereco" : "Rua Almirante Jequitir",
"IconeEmpresa" : "icone3.png",
"IconeCategoria": "hospital.png"
},
{
"Empresa" : "FarmaBem",
"Categoria" : "Farmacia",
"Tags" : [ "Venda de serviços" ],
"Endereco" : "Rua Sicranol",
"IconeEmpresa" : "icone4.png",
"IconeCategoria": "farmacia.png"
}
]
In this case for example the item "Category" has a limit on the possibilities of value (in this example I put 3 values, Hospital, Gas station and Pharmacy, if I were to insert a new record should add one of these 3 existing categories causing repetition) and the "Category" is directly linked to the "Iconecategory" and each category has a single corresponding icon.
My doubt and if there is a way to optimize these items, not to occur the risk of by some error be inserted a category with the different name (hospital type instead of Hospital) or if you have to change the logic or name of any category in the future you need to change line by line (Example, in the future it may be necessary to unify the "Category" Hospital and Pharmacy by creating a new "Category" Health).
But this is increasing the file size by repeating these values (categories.Hospital), replacing this (categories.Hospital) with a numerical ID and adding an ID in the dictionary, and when displaying the information read the dictionary by the ID.
– Carlos Fagiani Jr
I didn’t understand it at the end, in this case I’m only dealing with json without javascript because who will read the file would be a Java or Android system. But the idea of building a dictionary really opens up horizons, joining with the idea of @Carlosfagianijr’s replacement (categarias.Hospital) for an ID should definitely solve my problem
– Juliano Silveira
I think Carlos' idea is illogical, because the programmer will be lost, I would be, unless this ID came from a database. The for at the end was an example of how to access the data. But anyway I am happy to have solved :D
– Leonardo Bonetti
@Carlosfagianijr as you would know that ID == 1 is Hospital?
– Leonardo Bonetti
I got the question wrong, I thought he wanted to reduce the size of JSON, but he wanted to just organize the data, your answer is right. The way I suggested it would be like this: https://jsfiddle.net/u2zdwsxm/
– Carlos Fagiani Jr