Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

132
Visualizações
parse line-separated json objects in html table

I can't change json format. The data store in new line. json file:

{"ProgMode":"on","wait_h":"5","wait_m":"5","output":"1"}
{"ProgMode":"off","wait_h":"10","wait_m":"10","output":"2"}

I using below code but without bracket ([]) in json file, it doesn't work.

    var ReqJson = new XMLHttpRequest();
function response(){
    if(this.readyState == 4 && this.status == 200){
        var myObj = JSON.parse(this.responseText);
        const dbParam = JSON.stringify({table:"ProgramView",limit:20});
        let text = "<table class='table my-0'>"
        for (let x in myObj) {
        text += '<tr><td>' + myObj[x].wait_h + ':' + myObj[x].wait_m + ':' + myObj[x].output + '</td></tr>';
        }
        text += "</table>"    
        document.getElementById("dynamic_table").innerHTML = text;
    }
}

function ProccessConfig(){
    ReqJson.open("POST", "Programs.json", true);
    ReqJson.onreadystatechange = response;
    ReqJson.send()
}
ProccessConfig();

So how can I parse json that is stored with new lines and without comma and brackets?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can split the input text in rows and then 'JSON.parse' the single row.

let myObj = this.responseText.split('\n')
  .filter(line => line !== "")
  .map(JSON.parse);

Example based on your string:

let text = `{"ProgMode":"on","wait_h":"5","wait_m":"5","output":"1"}
{"ProgMode":"off","wait_h":"10","wait_m":"10","output":"2"}
`

let myObj = text.split('\n')
  .filter(line => line !== "")
  .map(JSON.parse);
console.log(myObj)

about 4 years ago · Juan Pablo Isaza Relatório

0

you don't need to split and map, it is to heavy for this case, try this

var myObj = JSON.parse(`[${this.responseText.replaceAll("}\n","},")}]`);

this is a little more complicated, but much more reliable,since it is not using any special symbols

var myObj = JSON.parse(`[{${this.responseText.substring(1).replaceAll("{",",{")}]`);
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda