0
\$\begingroup\$

I'm trying to create a script that parses a JSON file to get information about the levels in my project. I know this technique should work because I have the same code in two other scripts, parsing two other JSON files, and neither of them have any issues. But for some reason, something here is causing me to get an error:

ArgumentException: JSON parse error: Invalid value.
UnityEngine.JsonUtility.FromJson (System.String json, System.Type type) (at <f205f134343a4cd1bdc91ba88ac49a88>:0)
UnityEngine.JsonUtility.FromJson[T] (System.String json) (at <f205f134343a4cd1bdc91ba88ac49a88>:0)
LevelSelectScript.Start () (at Assets/Scripts/Menu Scripts/LevelSelectScript.cs:77)

Here's the code from LevelSelectScript (irrelevant bits cut out):

[SerializeField]
private TextAsset jsonFile;
[SerializeField]
private Levels lvlFromJson = new Levels();

void Start()
{
    jsonFile = Resources.Load<TextAsset>("JSON/levelsJSON");

    lvlFromJson = JsonUtility.FromJson<Levels>(jsonFile.text); //This is line 77, where the error occurs
}

The Levels class contains an array called levels[], which is made of Level objects. The Level class looks like this:

public class Level
{
    public string id;
    public string name;
    public bool challengeComplete;
}

And Levels looks like this:

public class Levels
{
    public Level[] levels;
}

And the JSON file looks like this:

{ "levels": [
    {   
        "id": "lvl0_lorem",
        "name": "Lorem",
        "challengeComplete": false
    },
    { 
        "id": "lvl1_ipsum",
        "name": "Ipsum",
        "challengeComplete": false
    }
]}

I'm sure I'm missing something really obvious that's present in my other scripts, but for the life of me I can't figure out what it is. As far as I can tell the JSON has proper syntax and its values match those of the Level class, which are the only areas I can think of that would produce the error.

\$\endgroup\$
4
  • \$\begingroup\$ The easiest solution is to use the reverse and save a json and compare it with the json you have. But it looks like your json is missing a level identifier somewhere \$\endgroup\$
    – Zibelas
    Commented Aug 19, 2023 at 4:54
  • \$\begingroup\$ Don't forget to show us the C# code for the Levels type. \$\endgroup\$
    – DMGregory
    Commented Aug 19, 2023 at 10:32
  • \$\begingroup\$ I recommend using Newtonsoft instead of JsonUtility: docs.unity3d.com/Packages/[email protected]/… \$\endgroup\$
    – Evorlor
    Commented Aug 19, 2023 at 21:15
  • \$\begingroup\$ I'm not able to reproduce this error with the code you've shown. The JSON deserializes correctly. Did you by any chance forget to include the [System.Serializable] attribute on your Level and Levels classes? If that's not it, can you edit your question to show us a minimal complete verifiable example, so we can isolate what the difference is between the working code I'm testing and the code that's throwing errors for you? \$\endgroup\$
    – DMGregory
    Commented Aug 19, 2023 at 23:27

1 Answer 1

1
\$\begingroup\$

In copying the JSON over, I cut out some of the numerous entries just to show what the format looked like without cluttering up the post. Well, in doing so I managed to crop out the last few lines, which looked like this:

    {  
        "id": "lvl10_lorem",
        "name": "lorem",
        "challengeComplete": false
    },
]}

Yup, an errant comma after the braces. Let this be a lesson to all those who come after: don't trim down your code to look pretty in a post!

\$\endgroup\$
1
  • \$\begingroup\$ Another way to avoid this problem is to take any code in your question and test it in a new, empty project to be sure it successfully reproduces the problem, since that's what your readers will have to do too. That helps you verify that you've included enough information for the problem to be unambiguously understood, and check that you've isolated just the relevant code/setup steps. \$\endgroup\$
    – DMGregory
    Commented Aug 22, 2023 at 16:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .