Search Unity

Getting "JSON parse error: Missing a name for object member." on FromJson

Discussion in 'Scripting' started by LightBulbTechnology, Mar 16, 2017.

  1. LightBulbTechnology

    LightBulbTechnology

    Joined:
    Apr 3, 2014
    Posts:
    5
    Started trying to learn how Unity handles Json files today. After some reading I started trying to put together a small system to read in a Json file following
    .

    On run, I am getting the following error:
    The TextAsset in my ItemCreator class below is properly filled as it is able to print the text to the log so I know I am overlooking something simple on JsonUtility.FromJson.

    Base item class:
    Code (CSharp):
    1. [System.Serializable]
    2. public class Item
    3. {
    4.         public int Number;
    5.         public string Type;
    6.         public double Speed;
    7. }

    Json item loader:
    Code (CSharp):
    1. public class PitchLoader : MonoBehaviour
    2. {
    3.     public Item myItem;
    4.  
    5.     void Awake()
    6.     {
    7.         string loadedItem = ItemCreator.LoadJsonAsResource("pitchList.json");
    8.         myItem = JsonUtility.FromJson<Item>(loadedItem);
    9.     }
    10. }

    Item Creator:
    Code (CSharp):
    1. public class ItemCreator
    2. {
    3.     // Use this for initialization
    4.     public static string LoadJsonAsResource(string path)
    5.     {
    6.         string jsonFilePath = path.Replace(".json", "");
    7.         TextAsset loadedJsonFile = Resources.Load<TextAsset>(jsonFilePath);
    8.         Debug.Log(loadedJsonFile.text);
    9.         return loadedJsonFile.text;
    10.     }
    11. }

    Json File:
    Code (Boo):
    1. {
    2.     "ItemNumber": 1,
    3.     "ItemType": "FT",
    4.     "ItemSpeed": 90.96,
    5. }
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    There's a comma at the end of your JSON file.
     
  3. LightBulbTechnology

    LightBulbTechnology

    Joined:
    Apr 3, 2014
    Posts:
    5
    New it was something simple but wow...

    Thanks man.
     
    parth16parikh likes this.
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    When in doubt, it's usually the JSON file's fault. ;)
     
    lilwooji, n2oukdnb and Bunny83 like this.
  5. parth16parikh

    parth16parikh

    Joined:
    Dec 17, 2015
    Posts:
    1
    Thanks I had the same issue!
     
  6. furroy

    furroy

    Joined:
    Feb 24, 2017
    Posts:
    93
    there are a number of online json formatting web sites. when i have an issue like this i copy/paste my json into one of those.
     
    abiodununity likes this.
  7. nishantjulian

    nishantjulian

    Joined:
    Aug 4, 2019
    Posts:
    1
    This saved me!! I spent all day trying to figure out where I was going wrong with the script and the answer was a single comma in the JSON file. Thank You!
     
  8. skwsk8

    skwsk8

    Joined:
    Jul 6, 2014
    Posts:
    35
    You might also get this if you put a comment somewhere in your json

    Code (CSharp):
    1. //comments aren't supported in json
     
  9. radiantboy

    radiantboy

    Joined:
    Nov 21, 2012
    Posts:
    1,633
    absolutely awful trying to do json in unity, even though I do it very often once in a while it takes me hours, so utterly annoying. just as I was about to paste my json and code, it somehow worked... i do wonder sometimes if its not compiling properly.
     
    Last edited: May 18, 2022
  10. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,999
    How is that relevant to this discussion of using malformatted json? Json has a strict and quite simple format as it is explained here. There can not be a trailing comma, neither in an object nor in an array. There may be json serializers which do not care, but strictly sticking to the format definition it's invalid json.

    Feel free to bookmark JsonLint.com. This page can validate your json and also beautifies it by applying some formatting rules.

    Yes, Unity's JsonUtility has some additional limitations. Specifically:
    • The top element has to be an object. No other json value is allowed / supported
    • Directly nested arrays are not supported. Having intermediate objects however does work
    • When it comes to object mapping, you actually need serializable classes to represent json objects and either arrays or Lists to represent arrays.
    Specifically point 2 is just an artifact of how Unity's own serialization system works and what constructs it supports.

    If you have a problem of your own, please start a new thread.
     
    Last edited: May 18, 2022