How to parse this JSON file?

How can I parse this JSON using unity’s JsonUtility.FromJson?

I’m not sure what kind of structure I need to create considering that there is more than 1 definition.

This the Json. It contains the various definition of the word “bottle”.

[
  {
    "partOfSpeech": "noun",
    "attributionText": "from Wiktionary, Creative Commons Attribution/Share-Alike License.",
    "sourceDictionary": "wiktionary",
    "text": "A container, typically made of glass and having a tapered neck, used for holding liquids.",
    "word": "bottle",
    "attributionUrl": "http://creativecommons.org/licenses/by-sa/3.0/",
    "wordnikUrl": "https://www.wordnik.com/words/bottle",
    "citations": [],
    "exampleUses": [],
    "labels": [],
    "notes": [],
    "relatedWords": [],
    "textProns": []
  },
  {
    "partOfSpeech": "noun",
    "attributionText": "from Wiktionary, Creative Commons Attribution/Share-Alike License.",
    "sourceDictionary": "wiktionary",
    "text": "The contents of such a container.",
    "word": "bottle",
    "attributionUrl": "http://creativecommons.org/licenses/by-sa/3.0/",
    "wordnikUrl": "https://www.wordnik.com/words/bottle",
    "citations": [],
    "exampleUses": [],
    "labels": [],
    "notes": [],
    "relatedWords": [],
    "textProns": []
  },
  {
    "partOfSpeech": "noun",
    "attributionText": "from Wiktionary, Creative Commons Attribution/Share-Alike License.",
    "sourceDictionary": "wiktionary",
    "text": "A container with a rubber nipple used for giving liquids to infants",
    "word": "bottle",
    "attributionUrl": "http://creativecommons.org/licenses/by-sa/3.0/",
    "wordnikUrl": "https://www.wordnik.com/words/bottle",
    "citations": [],
    "exampleUses": [],
    "labels": [],
    "notes": [],
    "relatedWords": [],
    "textProns": []
  },
  {
    "partOfSpeech": "noun",
    "attributionText": "from Wiktionary, Creative Commons Attribution/Share-Alike License.",
    "sourceDictionary": "wiktionary",
    "text": "<xref>Nerve</xref>, <xref>courage</xref>.",
    "labels": [
      {
        "text": "UK",
        "type": "region"
      },
      {
        "text": "informal",
        "type": "register"
      }
    ],
    "word": "bottle",
    "attributionUrl": "http://creativecommons.org/licenses/by-sa/3.0/",
    "wordnikUrl": "https://www.wordnik.com/words/bottle",
    "citations": [],
    "exampleUses": [],
    "notes": [],
    "relatedWords": [],
    "textProns": []
  },
  {
    "partOfSpeech": "noun",
    "attributionText": "from Wiktionary, Creative Commons Attribution/Share-Alike License.",
    "sourceDictionary": "wiktionary",
    "text": "With one's <xref>hair</xref> color produced by <xref>dyeing</xref>.",
    "labels": [
      {
        "text": "attributive",
        "type": "usage"
      }
    ],
    "word": "bottle",
    "attributionUrl": "http://creativecommons.org/licenses/by-sa/3.0/",
    "wordnikUrl": "https://www.wordnik.com/words/bottle",
    "citations": [],
    "exampleUses": [],
    "notes": [],
    "relatedWords": [],
    "textProns": []
  },
  {
    "partOfSpeech": "noun",
    "attributionText": "from Wiktionary, Creative Commons Attribution/Share-Alike License.",
    "sourceDictionary": "wiktionary",
    "text": "A bundle, especially of hay; something tied in a bundle.",
    "labels": [
      {
        "text": "obsolete",
        "type": "chronological"
      }
    ],
    "word": "bottle",
    "attributionUrl": "http://creativecommons.org/licenses/by-sa/3.0/",
    "wordnikUrl": "https://www.wordnik.com/words/bottle",
    "citations": [],
    "exampleUses": [],
    "notes": [],
    "relatedWords": [],
    "textProns": []
  },
  {
    "partOfSpeech": "verb",
    "attributionText": "from Wiktionary, Creative Commons Attribution/Share-Alike License.",
    "sourceDictionary": "wiktionary",
    "text": "To seal (a liquid) into a bottle for later consumption.",
    "labels": [
      {
        "text": "transitive",
        "type": "grammar"
      }
    ],
    "word": "bottle",
    "attributionUrl": "http://creativecommons.org/licenses/by-sa/3.0/",
    "wordnikUrl": "https://www.wordnik.com/words/bottle",
    "citations": [],
    "exampleUses": [],
    "notes": [],
    "relatedWords": [],
    "textProns": []
  },
  {
    "partOfSpeech": "verb",
    "attributionText": "from Wiktionary, Creative Commons Attribution/Share-Alike License.",
    "sourceDictionary": "wiktionary",
    "text": "To feed (an infant) <xref>baby formula</xref>.",
    "labels": [
      {
        "text": "transitive",
        "type": "grammar"
      },
      {
        "text": "UK",
        "type": "region"
      }
    ],
    "word": "bottle",
    "attributionUrl": "http://creativecommons.org/licenses/by-sa/3.0/",
    "wordnikUrl": "https://www.wordnik.com/words/bottle",
    "citations": [],
    "exampleUses": [],
    "notes": [],
    "relatedWords": [],
    "textProns": []
  },
  {
    "partOfSpeech": "verb",
    "attributionText": "from Wiktionary, Creative Commons Attribution/Share-Alike License.",
    "sourceDictionary": "wiktionary",
    "text": "To refrain from doing (something) at the last moment because of a sudden loss of courage.",
    "labels": [
      {
        "text": "UK",
        "type": "region"
      },
      {
        "text": "slang",
        "type": "register"
      }
    ],
    "word": "bottle",
    "attributionUrl": "http://creativecommons.org/licenses/by-sa/3.0/",
    "wordnikUrl": "https://www.wordnik.com/words/bottle",
    "citations": [],
    "exampleUses": [],
    "notes": [],
    "relatedWords": [],
    "textProns": []
  },
  {
    "partOfSpeech": "verb",
    "attributionText": "from Wiktionary, Creative Commons Attribution/Share-Alike License.",
    "sourceDictionary": "wiktionary",
    "text": "To strike (someone) with a bottle.",
    "labels": [
      {
        "text": "UK",
        "type": "region"
      },
      {
        "text": "slang",
        "type": "register"
      }
    ],
    "word": "bottle",
    "attributionUrl": "http://creativecommons.org/licenses/by-sa/3.0/",
    "wordnikUrl": "https://www.wordnik.com/words/bottle",
    "citations": [],
    "exampleUses": [],
    "notes": [],
    "relatedWords": [],
    "textProns": []
  }
]

You cannot.

Problems with Unity “tiny lite” built-in JSON:

In general I highly suggest staying away from Unity’s JSON “tiny lite” package. It’s really not very capable at all and will silently fail on very common data structures, such as Dictionaries and Hashes and ALL properties.

Instead grab Newtonsoft JSON .NET off the asset store for free, or else install it from the Unity Package Manager (Window → Package Manager).

https://assetstore.unity.com/packages/tools/input-management/json-net-for-unity-11347

Also, always be sure to leverage sites like:

https://jsonlint.com
https://json2csharp.com
https://csharp2json.io

Looks like a great solution but unfortunately it seems that this asset is not compatible with the newer versions of Unity.
There are some assembly exceptions on Unity v2021.3

JsonUtility.FromJson doesn’t support an array as root. You can add a little text at the front and back to turn things into an object with an array in it and then it will work. Not ideal, but a possible workaround. I think there are quite a few similar topics in the forum here.

Yes and no :slight_smile: The structure itself should be supported by Unity’s JsonUtility. However the JsonUtility does always expect an object as root element. So you can easily wrap the data in an object. The JsonUtility just has the same limitations as Unity’s serialization system and the data shown here should work when wrapped in an object.

Note: We can’t tell you what you should use for all those empty arrays as it’s not clear what their content may be. For example, the “labels” array contains objects with a “text” and “type” field. We don’t know what may be returned in those other arrays that are always empty in your example. You probably should consult the documentation of the API you’re using to know how the structure looks like. A single example most likely does not cover everything.

Though, maybe you don’t need everything. You can create objects which may only partially map the data that you’re interested in. So any additional data is simply ignored.

So it highly depends on what you actually need. If you just want to access certain fields, you could use my SimplyJSON parser which does not require that you create any custom classes. It just parses the structure into JSONObjects, JSONArrays and other classes for each type json supports. It just simplifies the way you can handle the data.