0

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);
});
    })

    })
9
  • 1
    You need to remove dataType: "json" - that tells jQuery to parse the response as JSON automatically, but that will of course fail when it is not valid JSON. Commented Jan 14, 2022 at 12:01
  • 1
    (For splitting at \n, you should be sure that none of the values can ever contain that kind of line break. Otherwise, this will fail or cause faulty data.) Commented Jan 14, 2022 at 12:03
  • 2
    The JSON from the server is not valid. Consider fixing it at the source? Commented Jan 14, 2022 at 12:03
  • 1
    Yes, it's not JSON, therefore it shouldn't use dataType: "json". Perhaps removing it makes jQuery guess wrong and it has to be explicitly set to "text". Commented Jan 14, 2022 at 12:09
  • 1
    Then it seems the data you're formatting has other issues. As @evolutionxbox suggested, this is usually easiest to fix at the source, so it produces valid JSON. Producing random data and trying to massage the format into JSON on the receiving end is usually doomed to failure. There are many potential problems. As a guess, you might have an extra newline at the end so the resulting string is something like [1, 2, 3, ] which will be invalid. Commented Jan 14, 2022 at 12:17

1 Answer 1

2

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);
});

Sign up to request clarification or add additional context in comments.

7 Comments

thanks for answering. l used like that, but still, l have noting to show from URL. l updated my question code above.
@AliGhassan most of my example is just for the demonstration/example. Either take the entire fetch-part and replace dataasfile with the real URL or take the line let jsonstr = [${text.split('\n').join(',')}]; and implement that in your jquery callback function.
When l used this code let jsonstr = [${text.split('\n').join(',')}] he will change it to vaild json . but he will put ' in the end so l need to remove it to get vaild json . [ {} ,]
check the json here jsoneditoronline.org/beta/… see the end of json befor },]
@AliGhassan I don't think that I see the same as you do in that link. There is a valid JSON doc to the left and nothing to the right. From your two comments, my guess is that there is a blank line in the end of the pseudo-JSON document.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.