← Back to blog saving and loading

JSON Serialization and Deserialization in Unity

by Vinod Ravisankar Jan 19, 2024
The cover for JSON Serialization and Deserialization in Unity

Data storage is really important in making video games. In Unity, there are many ways to save and get back game data. But the most common way is using JSON(JavaScript Object Notation) serialization and deserialization as it’s a lightweight data-interchange format. This article will help you learn how to use JSON serialization and deserialization in Unity.

Understanding JSON

JSON is a text-based format that is easy to read and write for humans and easy to parse and generate for machines. It’s used to transmit data between a server and a web application or between different components of an application. In Unity, JSON becomes a vital tool in handling data such as configurations, game states, or player data.

A JSON object is built on two structures:

  1. A collection of name/value pairs (often realized as a dictionary, hash table, or object in various languages).
  2. An ordered list of values (like an array, list, or sequence).

Example of a JSON object:

{
  "playerName": "Alex",
  "score": 1000,
  "levelsCompleted": [1, 2, 3, 4\]
}

JSON Serialization in Unity

Serialization is the process of converting an object into a format that can be easily saved or transmitted (in this case, JSON). In Unity, serialization can be easily done using the JsonUtility.ToJson method.

Steps to Serialize an Object:

  1. Create a serializable class that represents the data structure.
  2. Use JsonUtility.ToJson to convert an instance of this class into a JSON string.

Example:

[System.Serializable]
public class PlayerData 
{
  public string playerName;
  public int score;
  public int[] levelsCompleted;
}

PlayerData player = new PlayerData();
player.playerName = "Alex";
player.score = 1000;
player.levelsCompleted = new int[] {1, 2, 3, 4};
string json = JsonUtility.ToJson(player);

JSON Deserialization in Unity

Deserialization is the reverse process of serialization, where a JSON string is converted back into an object. This is done in Unity using JsonUtility.FromJson.

Steps to Deserialize a JSON String:

  1. Use JsonUtility.FromJson with the JSON string and the class type.
  2. Access the data from the returned object instance.

Example:

PlayerData playerData = JsonUtility.FromJson<PlayerData>(json);
name = playerData.playerName;

Best Practices for JSON in Unity

  1. Use Serializable Classes: Always create serializable classes for the data you want to serialize. Unity needs to know what data to serialize and deserialize.
  2. Handle Complex Data Carefully: Unity’s JsonUtility does not support serialization of game objects or other complex types directly. You might need to convert these into lists or arrays.
  3. Secure Sensitive Data: If you’re dealing with sensitive data, consider encrypting the JSON string before saving and decrypting it when loading.
  4. Use Pretty Print for Debugging: While saving or logging JSON data, use the pretty print option for easier debugging (JsonUtility.ToJson(obj, true)).

Conclusion

Understanding JSON serialization and deserialization in Unity is a fundamental skill for new developers. It empowers you to effectively manage game data, configurations, and player preferences. With the best practices in mind, you can ensure your data handling is efficient and secure, laying a strong foundation for your future projects in Unity.

Continue reading

Non-static method requires a targetHow to make a countdown timer in UnityPricing Strategy for Indie Game DevelopersUnity: How to Change Your SkyboxHow to add Friction to a Rigidbody in UnityUnity Standard Assets: The Essential GuideTop 10 Recent Games Developed with UnityUnity Sprite MasksHow to move or copy a Unity projectThe Ultimate Guide to Sun and Moon Positioning in Super Simple Skybox for Unity

Build your game with OccaSoftware

Learn on the go. Try OccaSoftware for free, and explore all the tools and assets you need to start, build, and launch your game.

Start now

Try OccaSoftware for free, no commitment required.