Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

130
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda