Skip to main content

Loading JSON Data

This project shows you how to load JSON data in two ways. First, from an asset in the project. Second, over HTTP from a remote server.

Loading JSON from an asset​


/**
* @attribute
* @title Character Data
* @type {Asset}
* @resource json
*/
characterData = null;

parseCharacterData(data) {
const names = [];

// Read the character data from the JSON asset and return a list of names
const characters = data.characters;
for (let character of characters) {
names.push(character.firstName + ' ' + character.lastName);
}

return names;
}

//...

// Get JSON data from a project asset
const characterData = this.characterData.resource;

// Parse JSON data
const names = this.parseCharacterData(characterData);

You can see in the code above that all you need to do to load JSON data from an asset in your project is to use a Script Attribute of type 'asset' or to retrieve the asset from the asset registry, then access the resource property. For an asset of type json the data will already be parsed into a standard javascript object when you access the resource property.

Once you have the javascript object you can access the data as normal. For example, looping through properties as in parseCharacterData.

Loading JSON from a remote server​

async loadJsonFromRemote(url, callback) {
const res = await fetch(url);
const json = await res.json();
callback(json);
}

//...

// load JSON from a remote server
this.loadJsonFromRemote("https://api.github.com/", (data) => {
// display JSON data from remote server
const el = document.querySelector("#xhr-json");
if (el) el.textContent = JSON.stringify(data, null, 4);
});

In this code we request JSON data from a URL, in this case the GitHub API, using the standard web browser API. The ESM version uses fetch and parses the response with res.json(). The Classic version uses an XMLHttpRequest and, after receiving the "load" event, parses the response with JSON.parse. Either way, the data is returned via the callback function.

Note, that the call to loadJsonFromRemote is asynchronous.

Here is the full code listing:

import { Script, Asset } from 'playcanvas';

export class Game extends Script {
static scriptName = "game";

/**
* @attribute
* @title Display
* @type {Asset}
* @resource html
*/
display = null;

/**
* @attribute
* @title Style
* @type {Asset}
* @resource css
*/
style = null;

/**
* @attribute
* @title Character Data
* @type {Asset}
* @resource json
*/
characterData = null;

// initialize code called once per entity
initialize() {
this.initDisplay();

// Get JSON data from a project asset
const characterData = this.characterData.resource;

// Parse JSON data
const names = this.parseCharacterData(characterData);

// display character names
let el = document.querySelector("#character-name");
if (el) el.textContent = names.join(", ");

// display JSON data from asset
el = document.querySelector("#asset-json");
if (el) el.textContent = JSON.stringify(characterData, null, 4);

// load JSON from a remote server
this.loadJsonFromRemote("https://api.github.com/", (data) => {
// display JSON data from remote server
el = document.querySelector("#xhr-json");
if (el) el.textContent = JSON.stringify(data, null, 4);
});
}

initDisplay() {
const el = document.createElement('style');
el.textContent = this.style.resource;
document.head.appendChild(el);

const div = document.createElement("div");
div.setAttribute("id", "container");
div.innerHTML = this.display.resource;
document.body.appendChild(div);
}

parseCharacterData(data) {
const names = [];

// Read the character data from the JSON asset and return a list of names
const characters = data.characters;
for (let character of characters) {
names.push(character.firstName + ' ' + character.lastName);
}

return names;
}

async loadJsonFromRemote(url, callback) {
const res = await fetch(url);
const json = await res.json();
callback(json);
}
}

Try the project for yourself.