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

136
Visualizações
Split new-line JSON into one array

I have a JSON from web server that looks like this:

{"timestamp":1642069251.6908009,"station_id":"ORBB","channel":5,"freq":131.725,"level":-28.1,"error":0,"mode":"2","label":"Q0","block_id":"8","ack":false,"tail":"A6-ANR","flight":"G90369","msgno":"S10A"}
{"timestamp":1642069257.00876,"station_id":"ORBB","channel":5,"freq":131.725,"level":-41.3,"error":0,"mode":"2","label":"Q0","block_id":"7","ack":false,"tail":"PH-HXO","flight":"HV6905","msgno":"S28A"}
{"timestamp":1642069259.057013,"station_id":"ORBB","channel":5,"freq":131.725,"level":-24.9,"error":0,"mode":"D","label":"Q0","block_id":"9","ack":false,"tail":"A6-ANR","flight":"G90369","msgno":"S11A"}

l have followed this answer but l have nothing to show in console.

l can't edit the data JSON above to deliver a valid JSON because it's coming from a data server using Ajax.

   $(document).ready(() => {
         $.ajax('acarsdec.json', {
        type: 'GET',
        //dataType: "text",
        timeout: 10000,
        cache: false,
        
}).done(function (data, textStatus, jqXHR) {
const dataasfile = `data:text/plain;base64,${btoa(data)}`;

fetch(dataasfile).then(res => res.text()).then(text => {
  let jsonstr = `[${text.split('\n').join(',')}]`;
  let json = JSON.parse(jsonstr);
  console.log(json);
});
    })

    })
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

As a starting point the piece of code that you show is not valid JSON, so there is no reason trying to parse it as JSON.

If you can trust that the source is "pseudo" JSON with one object per line, you can split the text on new line (\n) and join the string again with a comma (,) and put square brackets around the string:

let jsonstr = `[${text.split('\n').join(',')}]`;

Here I use the fetch function and a data URL to mimic the AJAX request.

/***** start setup for demostration *****/

const data = `{"timestamp":1642069251.6908009,"station_id":"ORBB","channel":5,"freq":131.725,"level":-28.1,"error":0,"mode":"2","label":"Q0","block_id":"8","ack":false,"tail":"A6-ANR","flight":"G90369","msgno":"S10A"}
{"timestamp":1642069257.00876,"station_id":"ORBB","channel":5,"freq":131.725,"level":-41.3,"error":0,"mode":"2","label":"Q0","block_id":"7","ack":false,"tail":"PH-HXO","flight":"HV6905","msgno":"S28A"}
{"timestamp":1642069259.057013,"station_id":"ORBB","channel":5,"freq":131.725,"level":-24.9,"error":0,"mode":"D","label":"Q0","block_id":"9","ack":false,"tail":"A6-ANR","flight":"G90369","msgno":"S11A"}`;

const dataasfile = `data:text/plain;base64,${btoa(data)}`;

/***** end setup for demostration *****/

/* replace dataasfile with your URL to the API */
fetch(dataasfile).then(res => res.text()).then(text => {
  let jsonstr = `[${text.split('\n').join(',')}]`;
  let json = JSON.parse(jsonstr);
  console.log(json);
});

Update

OP still having trouble parsing the string from the request. Apparently there is also a blank line in the end of the string. In the following example I added an blank line in the end of the string. I then filter the array, only accepting items in the array that are not an empty string. This line:

let jsonstr = `[${text.split('\n').filter(str => str != '').join(',')}]`;

/***** start setup for demostration *****/

const data = `{"timestamp":1642069251.6908009,"station_id":"ORBB","channel":5,"freq":131.725,"level":-28.1,"error":0,"mode":"2","label":"Q0","block_id":"8","ack":false,"tail":"A6-ANR","flight":"G90369","msgno":"S10A"}
{"timestamp":1642069257.00876,"station_id":"ORBB","channel":5,"freq":131.725,"level":-41.3,"error":0,"mode":"2","label":"Q0","block_id":"7","ack":false,"tail":"PH-HXO","flight":"HV6905","msgno":"S28A"}
{"timestamp":1642069259.057013,"station_id":"ORBB","channel":5,"freq":131.725,"level":-24.9,"error":0,"mode":"D","label":"Q0","block_id":"9","ack":false,"tail":"A6-ANR","flight":"G90369","msgno":"S11A"}
`; // blank line here in the end on purpose!

const dataasfile = `data:text/plain;base64,${btoa(data)}`;

/***** end setup for demostration *****/

/* replace dataasfile with your URL to the API */
fetch(dataasfile).then(res => res.text()).then(text => {
  let jsonstr = `[${text.split('\n').filter(str => str != '').join(',')}]`;
  let json = JSON.parse(jsonstr);
  console.log(json);
});

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