How to organize JSON content

Asked

Viewed 617 times

1

night!

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

Example:

Arquivo1.json

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

Arquivo2.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 look at the order, it’s different! I need to sort the file 2.json to be in the same order as Arquivo1.json, taking into account that the files contain more than 3000 lines when opening in notepadd++.

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

  • 1

    If your application depends on the order of the JSON data it probably has an architecture problem (or should not even use JSON, but some "fixed" format, such as plain text). I suggest rethinking the real problem, which is first interpreting JSON in your application, and then comparing the elements individually.

  • If you just want to compare in the text editor, select the 2 files and sort alphabetically. Then apply a diff to the files (but then the question may already be "almost out" :P of the site scope).

2 answers

1

First I would like to suggest an edition of your Json so that the return is readable, someone can tamper with the code that one day is not you! Then I’d do something about it: Write your file using Javascript if you don’t know how to provide the link https://www.w3schools.com/js/js_json.asp

 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 that would make a very simple sorting function:

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

I hope I helped strong abc

  • Gabriel Lucchese, thank you so much for responding and your concern!!! Excuse my ignorance more where I perform this? I’m not a javascript programmer, I just got the files. Can you give me more guidance please?

  • you must write this inside <script>//here tags all the way I passed it to you... or execute the code that the friend did here below, he already left the button ready for you! </script>

1

I guess I shouldn’t be doing this, but there it is.

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>

  • { "message": "Script error." , "filename": "", "lineno": 0, "colno": 0 } Tobias Mesquita..... I selected and this appeared

  • Your json is poorly formed, if you are using the example you posted, you need to remove the last comma.

Browser other questions tagged

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