[SOLVED] Getting Data from JSON

Hi all,
I’m struggling to understand an error I’m getting in relation to loading JSON data into an object.

I’m adding the JSON as an attribute (though I don’t want to do this long term) and tried directly accessing fields
with:

var data  = this.quizData.name;

this just prints the name of the file. So I tried:

var data  = this.quizData.resource;

In the console this does get the contents of the JSON object but also brings up a warning ‘Cannot read property ‘resource’ of null’ in the launch script. But it clearly can’t be null if it can read the file!

This is the whole class so it’s pretty basic:

var GameBuilder = pc.createScript('gameBuilder');

GameBuilder.attributes.add('quizData', {
    type: 'asset',
    assetType: 'json'
});

var GameShow;
var numOfRounds;
GameBuilder.prototype.initialize = function() {
    
    var data  = this.quizData.resource;
    console.log(data);
};

Is there a simpler way of accessing JSON data because this seems a bit convoluted.

Thanks,
Mark

This is the error:

launch.js:7491 TypeError: Cannot read property 'resource' of null
    at script.GameBuilder.initialize (GameBuilder.js?id=10916323:21)
    at Func._scriptMethod (playcanvas-stable.dbg.js:23059)
    at Func._onInitialize (playcanvas-stable.dbg.js:23076)
    at Func._callComponentMethod (playcanvas-stable.dbg.js:23325)
    at Func._onInitialize (playcanvas-stable.dbg.js:23331)
    at Function.fire (playcanvas-stable.dbg.js:608)
    at Function.initialize (playcanvas-stable.dbg.js:21097)
    at Application.start (playcanvas-stable.dbg.js:20645)
    at launch.js:7019
    at Application._preloadScripts (playcanvas-stable.dbg.js:20489)

Yet just underneath this is:

{name: "Test Quiz", numOfRounds: 3, numOfTeams: 0, firstRound: {…}, secondRound: {…}, …}

Link to scene, please?

https://playcanvas.com/editor/scene/583745

That generates a 404. Presumably that’s a scene from a private project?

Ah yeah, whats the best way to share it?

I just need a link to a public scene that reproduces the problem. Ideally, this scene should be a simple, cut-down repro. Otherwise, it’s tough to debug.

OK, I’ll set a public scene when i get a minute.

Just to let you know where I’m at now this code:

GameShow = this.quizData;
console.log(GameShow.resource.name);

produces this error & the correct answer at the bottom, so my immediate assumption is that it is some kind of loading order issue:

playcanvas-stable.js:22912 unhandled exception while calling "initialize" for "gameBuilder" script:  TypeError: Cannot read property 'resource' of null
    at script.GameBuilder.SetJSON (GameBuilder.js?id=10916323:31)
    at script.GameBuilder.initialize (GameBuilder.js?id=10916323:26)
    at Func._scriptMethod (playcanvas-stable.js:22908)
    at Func._onInitialize (playcanvas-stable.js:22925)
    at Func._callComponentMethod (playcanvas-stable.js:23174)
    at Func._onInitialize (playcanvas-stable.js:23180)
    at Function.fire (playcanvas-stable.js:606)
    at Function.initialize (playcanvas-stable.js:20950)
    at Application.start (playcanvas-stable.js:20503)
    at launch.js:7019
_scriptMethod @ playcanvas-stable.js:22912
_onInitialize @ playcanvas-stable.js:22925
_callComponentMethod @ playcanvas-stable.js:23174
_onInitialize @ playcanvas-stable.js:23180
fire @ playcanvas-stable.js:606
initialize @ playcanvas-stable.js:20950
start @ playcanvas-stable.js:20503
(anonymous) @ launch.js:7019
_preloadScripts @ playcanvas-stable.js:20347
(anonymous) @ launch.js:7002
done @ playcanvas-stable.js:20238
onAssetLoad @ playcanvas-stable.js:20251
fire @ playcanvas-stable.js:606
(anonymous) @ playcanvas-stable.js:31036
(anonymous) @ playcanvas-stable.js:28890
onLoaded @ playcanvas-stable.js:30104
(anonymous) @ playcanvas-stable.js:28890
image.onload @ playcanvas-stable.js:29725

launch.js:7491 TypeError: Cannot read property 'resource' of null
    at script.GameBuilder.SetJSON (GameBuilder.js?id=10916323:31)
    at script.GameBuilder.initialize (GameBuilder.js?id=10916323:26)
    at Func._scriptMethod (playcanvas-stable.js:22908)
    at Func._onInitialize (playcanvas-stable.js:22925)
    at Func._callComponentMethod (playcanvas-stable.js:23174)
    at Func._onInitialize (playcanvas-stable.js:23180)
    at Function.fire (playcanvas-stable.js:606)
    at Function.initialize (playcanvas-stable.js:20950)
    at Application.start (playcanvas-stable.js:20503)
    at launch.js:7019
console.error @ launch.js:7491
_scriptMethod @ playcanvas-stable.js:22913
_onInitialize @ playcanvas-stable.js:22925
_callComponentMethod @ playcanvas-stable.js:23174
_onInitialize @ playcanvas-stable.js:23180
fire @ playcanvas-stable.js:606
initialize @ playcanvas-stable.js:20950
start @ playcanvas-stable.js:20503
(anonymous) @ launch.js:7019
_preloadScripts @ playcanvas-stable.js:20347
(anonymous) @ launch.js:7002
done @ playcanvas-stable.js:20238
onAssetLoad @ playcanvas-stable.js:20251
fire @ playcanvas-stable.js:606
(anonymous) @ playcanvas-stable.js:31036
(anonymous) @ playcanvas-stable.js:28890
onLoaded @ playcanvas-stable.js:30104
(anonymous) @ playcanvas-stable.js:28890
image.onload @ playcanvas-stable.js:29725

GameBuilder.js?id=10916323:31 
Test Quiz

I’ve made a public version with the code in:
https://playcanvas.com/editor/scene/584625

but it’s not causing the same error in the launch script. So I’m not sure what to do at the moment

Seems to run fine for me… :thinking:

Yeah, I’m not sure what to do at the moment. Whether to make a new project and try and re-write everything or see if I can find a way around it. Though I’m not overly confident about that as the issue seems to be in the launch script.

Just add me to your project temporarily. Username: ‘will’.

I’ve managed to find an alternative way to get to the JSON, I’m using:

this.app.assets.findByTag("quizJSON")[0].resource

this seems to be working for now so I’ll just go with that.

Thanks

1 Like

I was struggling with this too and it seems the problem was that I didn’t set my JSON file to ‘Preload’ in the editor. Just leaving that here for someone else to find