Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

127
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!