How to organize JSON content [closed]

1

night!

I need your help on the following question: I have 2 .json files, but when I open it in Notepad ++ I realize that the content is disorganized.

Example:

File1.json

{
"xxx.yyy.E-mail": "E-mail",
"xxx.yyy.Senha": "Senha",
"xxx.yyy.Voltar": "Volver",
"xxx.yyy.Enviar": "Enviar",
"xxx.yyy.Sair"  : "Sair",
}

File2.json

{
"xxx.yyy.E-mail": "E-mail",
"xxx.yyy.Voltar": "Volver",
"xxx.yyy.Senha": "Senha",
"xxx.yyy.Sair"  : "Sair",
"xxx.yyy.Enviar": "Enviar",
}

If you observe the order is different! I need to sort the 2.json file so that it is in the same order as File1.json, this taking into account that the files contain more than 3000 lines when opening in notepadd ++.

Is there any way to sort the json content? I need to organize and then compare the contents line by line. Thanks in advance!!!

    
asked by anonymous 10.10.2017 / 00:03

2 answers

1

First of all I'd like to suggest an edition of your Json so that the return is legible, someone can tinker with the code that one day is not you! Then he would do something about it:    Write your file using Javascript, if you do not know how to make the link available     link

 var Json =    
{
"xxx.yyy.E-mail": "E-mail",
"xxx.yyy.Senha": "Senha",
"xxx.yyy.Voltar": "Volver",
"xxx.yyy.Enviar": "Enviar",
"xxx.yyy.Sair"  : "Sair",
 }

After this I would do a very simple sort function:

function OrdenarPorNome()
{
   Json.sort(function (a, b) { return a.Json > b.Json ? 1 : -1; });
} 

I hope to have helped strong abc

    
10.10.2017 / 00:27
1

I guess I should not be doing this, but it's there.

var arquivo_json = document.getElementById("arquivo_json");
var ordenar = document.getElementById("ordenar");
var form = document.getElementById("form");

ordenar.addEventListener("click", function (evt) {
  if (form.checkValidity())
  {
    evt.preventDefault();
    var reader = new FileReader();
    reader.addEventListener("load", function (evt) {
      var json = JSON.parse(evt.target.result);
      var novo = {};
      Object.keys(json).sort().forEach(function (key) {
        novo[key] = json[key];
      })
      var blob = new Blob(
        [JSON.stringify(novo, null, 2)], 
        { type: "application/json" }
      );
      var link = document.createElement("a");
      link.href = URL.createObjectURL(blob);
      link.download = arquivo_json.files[0].name;
      link.textContent = "download " + link.download + " ordenado";
      document.body.appendChild(link);
    });
    reader.readAsText(arquivo_json.files[0]);
  }
})
label {
line-height: 30px;
}
<form id="form" action="#">
  <label>
    Arquivo JSON: 
    <input id="arquivo_json" name="arquivo_json" required type="file" />
  </label>
  <input id="ordenar" name="ordenar" type="submit" value="Ordenar" />
</form>
    
10.10.2017 / 03:14