Jump to content

You're browsing the 2004-2023 VATSIM Forums archive. All content is preserved in a read-only fashion.
For the latest forum posts, please visit https://forum.vatsim.net.

Need to find something? Use the Google search below.

Javascript parser for v3 json help


Othniel Alexandre
 Share

Recommended Posts

Othniel Alexandre
Posted
Posted

Anyone able to share their javascript parser for the vatsim json v3 data feed. I am in my first month of java scripting and I am complete lost with this structure. It is so nested but the biggest challenge I seem to be facing is data type conversions going from utf8 and to byte and to string and json. I have no clue which should come first or when. My latest rendition looks like this: I think I drilled into the data far enough to find the real content but it looks really funky which leads me to believe there is some type of data conversion issue.

let url = 'https://data.vatsim.net/v3/vatsim-data.json';
    let results;
    await fetch (url, {'method''get'})
    .then((httpResponse) => {httpResponse.json()
        results = httpResponse
        })
   
    let decoded = utf8Decode(JSON.stringify(results))
    let rawdata = JSON.parse(decoded)
    let thedata = bin2string(rawdata.body._outBuffer.data)
    console.log(thedata)
    
the data looks like this:
 
 
isFulfilled:
true
 
isRejected:
false
 
fulfillmentValue:
"{\"general\":{\"version\":3,\"reload\":1,\"update\":\"20210328201544\",\"update_timestamp\":\"2021-03-28T20:15:44.9810021Z\",\"connected_clients\":1526,\"unique_users\":1465},\"pilots\":[{\"cid\":1451563,\"name\":\"Marco Dubeux SBRF\",\"callsign\":\"ANZ756UUUUUUUUUUUUUUUU\u0004\u0000\u0000\u0000\u0000(\u001cº\u0004\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000ified: Sun, 28 M\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u000020:15:47 GMT\r\nET\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000060e3f3-11025e\"\r\u0000\u0000\u0000\u0000\u000...

I can tell that the fulfillmentValue tag has the actual data, but why it shows up the way it does is beyond me.

I know your time is valuable, but any help is much appreciated.

Link to comment
Share on other sites

Ross Carlson
Posted
Posted

I don't have a lot of experience with JS, so I'm probably missing something, but it seems to me that you are parsing the feed twice. The httpResponse.json() call parses it into an object, then you serialize it back into a JSON string, and then parse it back into an object. Why parse it twice?

Developer: vPilot, VRC, vSTARS, vERAM, VAT-Spy

Senior Controller, Boston Virtual ARTCC

Link to comment
Share on other sites

Othniel Alexandre
Posted
Posted

The utf8converter requires string so I have to convert it to string, then the output from that I need in json to access the key/value pairs needed.

 

At least that is my understanding.

Link to comment
Share on other sites

Ross Carlson
Posted
Posted

Why do you need to do UTF8 conversion?

Developer: vPilot, VRC, vSTARS, vERAM, VAT-Spy

Senior Controller, Boston Virtual ARTCC

Link to comment
Share on other sites

Mark Doyle
Posted
Posted

This isn't really specific to the datafeed, but here is a basic snippet of how to do it, refreshing data every 30s. Check out the MDN Docs on async/await.

Keep in mind that you **cannot** get data from the datafeed from a browser. You have to do this from your own server and then pass the data onto your clients. This is to reduce the amount of requests to the datafeed. You could use node.js to create a simple server for this. Anyway:

const fetchData = async () => {
    try {
        const response = await fetch("https://data.vatsim.net/v3/vatsim-data.json");
        return response.json();
    } catch(error) {
        console.log("Error fetching data:", error);
    }
};

const updateData = () => {
    fetchData();

    setTimeout(async () => {
        const data = await fetchData();
        console.log(data);
    }, 30000);
};

updateData();

 

Developer: vatsim.net | community.vatsim.net | estvacc.org

                                                           vatsim tech logo
   
                                               

 

Link to comment
Share on other sites

 Share