Getting an error while loading JSON from resources folder in WebGL build

While running on the editor, the strings stored in JSON is able to load, but when I am running on browser it’s giving an error. I have tried to look for some solution but nothing good. Sharing a screenshot of the error and the script where I am loading the JSON.

using UnityEngine;

public static class JsonLoader
{
    public static string LoadJson(string loadPath)
    {
        TextAsset data = Resources.Load<TextAsset>(loadPath);
        return data.text;
    }
}

What’s the actual path?

The load path? It’s the name of the JSON file stored in resources folder

So loadPath is something like this: “myfile.json”?

Hmmm I don’t think the path is the issue. I looked at the error again and noticed it fails to create a List, and that you are using Newtonsoft Json. Are you using the official Newtonsoft package? If not, I would try that. It may be more compatible with WebGL.

Unless you have specific requirements you can also use JsonUtility, it has some limits but if all you need to do is load a list of strings for instance, it’ll be fine for that as long as the list is part of a serialized class. JsonUtility cannot serialize collections directly.

So I have an array of strings in my JSON which I am loading into a Queue. I am using official Newtonsoft package. No the list is not part of a serialized class. Let me show how I am loading the data into the queue, maybe it can give more context

A snippet of code, but you can see I am using "DetectiveBegin.json" to load into the string, which is deserialized into the queue!

using UnityEngine;
using Newtonsoft.Json;
using System.Collections.Generic;

namespace MysteryMayhem.Dialogue
{
    public class DialogueLoader : MonoBehaviour
    {
        #region ---------- Public Methods ----------
        public Queue<string> GetDetBeginQueue()
        {
            string detString = JsonLoader.LoadJson("DetectiveBegin");
            Queue<string> detectiveQueue = JsonConvert.DeserializeObject<Queue<string>>(detString);
            return detectiveQueue;
        }

I see. It would be helpful to know whether the exception occurs during LoadJson or DeserializeObject.

If you haven’t already: enable full stacktrace Exception handling in the Player Settings, this is near the bottom of all the settings.

But here you could just Debug.Log(detString) and see if this appears or not.
You could also try, for a test, to deserialize it into List instead. I’m assuming it wouldn’t matter because a Queue is just a List in Json format. If this works, you could then initialize a Queue with the contents of the List.

1 Like

Thanks so much @CodeSmile for the advise, it worked. Yes the problem was during Deserialization of object. I stored it in List and then put in Queue to load, it worked fine.