VOOZH about

URL: https://www.geeksforgeeks.org/c-sharp/json-serialization-in-unity/

⇱ JSON Serialization In Unity - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

JSON Serialization In Unity

Last Updated : 4 May, 2026

PlayerPrefs can only save numbers and strings. But what if you need to save a player's inventory, position, health and quest progress, then JSON solves this by converting complex data into text format. JSON (JavaScript Object Notation) is a text format that stores structured data. It looks like this:

JSON is simple, readable, and Unity can convert your C# classes to JSON automatically.

When to Use JSON

  • Saving game state: Stores position, health, and inventory together.
  • Player inventory: Stores a list of items.
  • Quest progress: Stores multiple quests with different states.
  • Character stats: Stores level, XP, skills, and equipment.

JSON is perfect when you need to save many different values as one file.

Step 1: Create a Save Data Class

First, create a class that holds all the data you want to save.

[System.Serializable] is required. It tells Unity this class can be converted to JSON.

Step 2: Save Data to JSON

Convert your class object into a JSON string and save it.

JsonUtility.ToJson() converts your class into a JSON string. Then save that string using PlayerPrefs.

Step 3: Load Data from JSON

Retrieve the JSON string and convert it back to your class.

JsonUtility.FromJson<GameData>() converts the JSON string back into a GameData object.

Complete Working Example

Here's a complete save system for a player:

JSON vs PlayerPrefs (Direct)

JSON + PlayerPrefs

  • Save multiple values using a single key.
  • Supports complex data (lists, arrays, objects).
  • Easy to read and debug.
  • Cleaner code with one Save/Load system.

PlayerPrefs alone

  • Requires one key per value.
  • Does not support complex data.
  • Harder to read and debug.
  • Leads to many Get/Set lines.
Comment
Article Tags:
Article Tags:

Explore