Importing GLB/GLTF model causes JSON Parse Error

I’m trying to load some models from a remote URL into my app and I’m suddenly getting this error over and over again:

SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

The models I’m working with are those available from THREE.js’s GIT - here - so they’re all perfectly good legit models that we’ve all been using for years.

What’s weird is that I first downloaded them to my hard drive and then loaded them into my project directly from there - and my code worked perfectly. But as soon as I uploaded these very same models to a remote server and tried to load them from that URL, I suddenly started getting this error.

Here’s my loading function:

function loadModel(modelFileName) {
	console.log("->In 'loadModel()', 'modelFileName' =", modelFileName);

	// Load the Object:
    gltfLoader.load(modelFileName, function(incomingScene) { 
        console.log(modelFileName, "' has arrived!!!");
        console.log("  =>'incomingScene' = ", incomingScene);

        let loadedScene = new THREE.Scene();
	    loadedScene = incomingScene.scene;
                    
        scene.add(loadedScene);
        const defaultTransform = new THREE.Matrix4()
              .multiply(new THREE.Matrix4().makeScale(0.5, 0.5, 0.5));
        loadedScene.applyMatrix4(defaultTransform);
    },
    // Called when loading is in progresses:
    function(xhr) {
          console.log( (xhr.loaded / xhr.total * 100) + '% loaded' );
    },
    function(error) {
          console.log("ERROR! -->", error);
    });

}

Here’s one of the URL’s at which the model I’m requesting - the Flamingo.glb model - is sitting:
https://ipfs.io/ipfs/QmYqwNYxqmu4z39emTo7h9D62rbwm1esAmbAf2PctAyUvu?filename=Flamingo.glb

(What’s even weirder is that this particular model was actually working just fine when I first loaded it from that URL, but then it suddenly stopped loading and gave me that error I listed up top. Makes no sense.)

Any ideas what’s causing this - and how it can be solved?

Looks OK here: glTF Viewer

When you see this error, there should usually be more errors in your JS console or in the Network tab of your browser’s developer tools. Those might have more information.

OK I looked in both the Console and Network tabs - the only additional info is from my progress callback - meaning this bit of code:

// Called when loading is in progresses:
    function(xhr) {
          console.log( (xhr.loaded / xhr.total * 100) + '% loaded' );
    },

And what the console logs from that is this:

Infinity% loaded

Does that shed any light?

I see all sorts of questions posted about this error on StackOverflow, but only one having to do with THREE.js in particular - and that one isn’t helpful.

What could be causing this error? What could it mean? (we all know there’s no “unexpected character” in this - or any of the other glb files that are on THREE js’s GIT repo…)

I posted this same question on StackOverflow - here: 3d - THREE.js Model-Loading Issues - Stack Overflow

Is there a proper way to link these two URL’s so everyone knows it’s the same post?

@donmccurdy Hey you were right about the “Network” tab - I took a much closer look, and it seems the browser IS thinking that the “Flamingo.glb” file I’m trying to load is an HTML file, even though it’s clearly not.

Here’s what it’s showing:

The highlighted “QmauYb8…” - that’s part of the ipfs link for the model, and on the left side you can see it’s listing it as an “html” file.

To contrast, when I load the exact same file directly from my hard-drive, the Network tab shows it to be of type "octet-stream".

So why is this happening?
And more importantly, what do I need to do to fix this?

If you click right click and access that URL, then visit it, what do you actually get?

This error usually means the file doesn’t exist at that URL, and that your web server is giving you something else — like an HTML “Not Found” page — instead. How to fix this depends entirely on how your project and folder structure is set up.

@donmccurdy
Ok, it seems the URL gets all chopped up in the Network tab of my browser’s Console - check it out:

Screen Shot 2021-03-05 at 7.59.00 PM

It’s split into pieces - “Scheme”, “Host”, “Filename” - and ultimately “Filename” points to the FOLDER in which the “.glb” file is sitting, instead of the actual “.glb” file itself.
So it’s basically ignoring the very last part of the URL, which is this:

?filename=Flamingo.glb

As a reminder, the full URL is actually this:
https://ipfs.io/ipfs/QmYqwNYxqmu4z39emTo7h9D62rbwm1esAmbAf2PctAyUvu?filename=Flamingo.glb

But nowhere in the Networks tab is this URL showing up as a single full URL that I can actually click on and navigate to.
This is happening in both Chrome and Firefox.

Not sure why it’s doing this.

Is there a way to “force” the browser to not ignore that last ?filename=Flamingo.glb portion of the url?

Or some other work-around?

These questions all relate to your project, and how your code is trying to load this model. Without some way to test and run that code I don’t think that I can help further.

@donmccurdy OK I just created a quick fiddle and the model loads there perfectly - from the very same URL that’s failing in my website’s code (“https://ipfs.io/ipfs/QmYqwNYxqmu4z39emTo7h9D62rbwm1esAmbAf2PctAyUvu?filename=Flamingo.glb”) , so this makes no sense.

https://jsfiddle.net/gilomer88/m1odvtgz/6/

At the end of the day the code I’m using to load the model into my website is exactly as I posted it in my original question up top. I mean you’ve seen it. I’m about 99.9% sure there’s anything wrong with it. So it’s gotta be some kind of quirky browser issue.

Weird.

Anyway, I’ll figure something out.

Thanks for the ongoing help.
I gotta say I’m totally psyched about THREE.js - I just love it. And can’t believe I didn’t discover it till like 6 months ago (or whenever it was.)
So expect to see many more questions from posted on this forum!

Thanks again!

1 Like

did you get it working. there are no working solutions i have found. this is pretty ridiculous. i’m seeing other people load their models just fine but mine is not… pretty annoying. there should be documentation on this error

Error is anecdotal, as far as I can tell. The loader is just not understanding what it is you are passing to it. It is asynchronously attempting to load what is essentially nothing, therefore ‘Infinite’. I could be wrong, but just don’t focus on it that’s all.

You may have to sanitize the URL input by removing any unwanted characters passed along to the GLTF Loader that are unintended. Since its a function you created, it may be coming from a different source too.

I’d start with attempting to load the asset in a basic environment on its own with just the GLTF loading function from the docs and see what that returns.

The times I get this error is usually a problem with the file path and nothing else.

For instance,

gltfLoader.setPath('../models') // this path returns json parse error
gltfLoader.load('asset.glb', (gltf) => { ... })

/* ------------ */

gltfLoader.setPath('../models/') // this path works, note the '/'
gltfLoader.load('asset.glb', (gltf) => { ... })


gltfLoader.setPath('../models') // this path works too
gltfLoader.load('/asset.glb', (gltf) => { ... }) // cause the '/' is here instead

@smellycyborg
as per @blinnk 's suggestion like this should work like this …

	const loader = new GLTFLoader();
	loader.setPath('https://ipfs.io/ipfs/')
	loader.load( "QmYqwNYxqmu4z39emTo7h9D62rbwm1esAmbAf2PctAyUvu", function ( gltf ) {
		flamingoMesh = gltf.scene.children[ 0 ];
    flamingoMesh.rotation.y = -15;
    scene.add(flamingoMesh);
	} );

thanks for the backup soldier!