0
0
Unityframework~10 mins

JSON serialization in Unity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - JSON serialization
Create Object
Convert Object to JSON String
Use JSON String (Save/Send)
Convert JSON String back to Object
Use Object in Program
This flow shows how an object is turned into a JSON string and back, so it can be saved or sent and then used again.
Execution Sample
Unity
using UnityEngine;

[System.Serializable]
public class Player {
  public string name;
  public int score;
}

public class Example : MonoBehaviour {
  void Start() {
    Player p = new Player { name = "Anna", score = 10 };
    string json = JsonUtility.ToJson(p);
    Debug.Log(json);
  }
}
This code creates a Player object, converts it to a JSON string, and prints the JSON string.
Execution Table
StepActionObject StateJSON StringOutput
1Create Player objectname="Anna", score=10
2Serialize object to JSONname="Anna", score=10{"name":"Anna","score":10}
3Print JSON stringname="Anna", score=10{"name":"Anna","score":10}{"name":"Anna","score":10}
4Deserialize JSON back to objectempty Player object{"name":"Anna","score":10}name="Anna", score=10
5Use deserialized objectname="Anna", score=10Player name: Anna, score: 10
💡 Serialization and deserialization complete, object restored from JSON string.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 4Final
p (Player object)nullname="Anna", score=10name="Anna", score=10name="Anna", score=10name="Anna", score=10
json (string)""""{"name":"Anna","score":10}{"name":"Anna","score":10}{"name":"Anna","score":10}
Key Moments - 3 Insights
Why do we need the [System.Serializable] attribute on the Player class?
Unity requires [System.Serializable] to know that the Player class can be converted to JSON. Without it, JsonUtility.ToJson will not work (see Step 2 in execution_table).
Does the JSON string contain the exact same data as the object?
Yes, the JSON string is a text version of the object's data (Step 2 and Step 3). It does not include methods or behavior, only data fields.
What happens if we change the JSON string before deserializing?
If the JSON string is changed to valid JSON with matching fields, the deserialized object will reflect those changes (Step 4). Invalid JSON causes errors.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'json' after Step 2?
A{"name":"Anna","score":10}
Bname="Anna", score=10
Cnull
D""
💡 Hint
Check the 'JSON String' column in row for Step 2 in execution_table.
At which step is the Player object first created with data?
AStep 4
BStep 1
CStep 3
DStep 5
💡 Hint
Look at the 'Action' and 'Object State' columns in execution_table for Step 1.
If we remove [System.Serializable] from Player class, what happens at Step 2?
AJsonUtility.ToJson returns the full object data
BThe program crashes immediately
CJsonUtility.ToJson returns an empty JSON string
DThe JSON string contains method names
💡 Hint
Refer to key_moments about the importance of [System.Serializable] and Step 2 in execution_table.
Concept Snapshot
JSON serialization in Unity:
- Mark classes with [System.Serializable]
- Use JsonUtility.ToJson(object) to convert to JSON string
- Use JsonUtility.FromJson<T>(json) to convert back
- JSON stores data only, no methods
- Useful for saving or sending data
Full Transcript
In Unity, JSON serialization means turning an object into a JSON string and back. First, you create an object with data. Then, you use JsonUtility.ToJson to convert it into a JSON string, which is text that represents the object's data. This string can be saved or sent somewhere. Later, you can convert the JSON string back into an object with JsonUtility.FromJson. The class must have the [System.Serializable] attribute so Unity knows it can be serialized. The JSON string only contains data fields, not methods or behavior. This process helps store or share data easily.