Compressing and organizing Json

Asked

Viewed 254 times

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).

1 answer

3


My solution was to create a dictionary with the categories, so you do not need to fill the company object with attributes that belong 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, just insert a new category object in the dictionary categories with their respective properties inside. Already in the construction of the object empresa in the attribute Categoria just pass one of the existing objects inside the category dictionary.

Follow a practical test of how it works, any doubt I’m available.

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);
}

  • 1

    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.

  • 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

  • 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

  • @Carlosfagianijr as you would know that ID == 1 is Hospital?

  • 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/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.