Compressing and organizing Json

1

I've been studying Json recently to use my projects to replace databases in some cases, in my tests I found a doubt. depending on the project I can 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 registry it should add one of these 3 existing categories causing the repetition) in addition the "Category" is directly linked to the "IconCategory" and each category has a single corresponding icon.

My question is, if there is a way to optimize these items, so that there is no risk that a category with a different name (hospital type instead of Hospital) will be inserted or if you change the logic or the name of any category in the future needs 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).

    
asked by anonymous 14.11.2017 / 14:24

1 answer

3

My solution was to create a dictionary with the categories, so you do not have to fill the company object with attributes belonging to categoria :

var categorias = {
    Hospital:{
      descricao: "Hospital",
      icone:"hospital.png"
    },
    PostoDeGasolina:{
      descricao: "Posto de Gasolina",
      icone:"posto.png"
    },
    Farmacia:{
      descricao: "Farmácia",
      icone:"farmacia.png"
    }
};

If you need to add a new category, simply insert a new category object into the dictionary with its properties within it. In the construction of the empresa object in the Categoria attribute, simply pass one of the existing objects within the category dictionary.

Here's a practical test of how it works, any questions I'm having.

var categorias = {
    Hospital:{
      descricao: "Hospital",
      icone:"hospital.png"
    },
    PostoDeGasolina:{
      descricao: "Posto de Gasolina",
      icone:"posto.png"
    },
    Farmacia:{
      descricao: "Farmácia",
      icone:"farmacia.png"
    },
};

var empresas = [
  {
    Empresa       : "Unimed",
    Categoria     : categorias.Hospital,
    Tags          : ["Plano de saude" , "Clinicas diversas", "Venda de serviços" ],
    Endereco      : "Rua Fulano de Tal",
    IconeEmpresa  : "icone.png",
  },
  {
    Empresa       : "Ipiranga",
    Categoria     : categorias.PostoDeGasolina,
    Tags          : ["Venda de Produtos", "Venda de serviços" ],
    Endereco      : "Rua Fulano",
    IconeEmpresa  : "icone2.png",
  },
  {
    Empresa       : "Beneficente",
    Categoria     : categorias.Hospital,
    Tags          : ["Plano de saude" , "Venda de serviços" ,"Medico Especializado"],
    Endereco      : "Rua Almirante Jequitir",
    IconeEmpresa  : "icone3.png",
  },
  {
    Empresa       : "FarmaBem",
    Categoria     : categorias.Farmacia,
    Tags          : [ "Venda de serviços" ],
    Endereco      : "Rua Sicranol",
    IconeEmpresa  : "icone4.png",
  }
];

var i =0;
for(i = 0; i < empresas.length; i ++){
  console.log(empresas[i].Categoria.descricao);
}
    
14.11.2017 / 14:53