Creating and Reading JSON in Unity

Creating and Reading JSON in Unity

Creating and Reading JSON in Unity

 Hello everyone, in this post I will explain how to create a JSON file with Unity and how to read the file we created. 

Actually, there are 3 articles about this on our site, but the codes we write in the software field are constantly changing, that's why I'm rewriting this topic now. 

The codes I wrote in the past were working, but the method I will explain here is much simpler and much more reusable compared to the others.

If you want to check out the old articles, you can use the links below.

Script for Creating and Reading JSON with Unity

First, let's write our code for reading and writing and then see how we will use it.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System; // Our library necessary for file operations.

public class UBJsonDataManager : MonoBehaviour
{
    public static dynamic ReadJSONData(string dataPath, Type dataType) // We create a function for reading JSON, we require the file path and type.
    {
        string jsonData = System.IO.File.ReadAllText(dataPath); // We read our file
        dynamic returnedData = JsonUtility.FromJson(jsonData, dataType); // We convert the file we read from JSON to the desired type.
        return returnedData; // We return our file.
    }


    public static void SaveJSONData(dynamic data, string dataPath) // We create our function to create a JSON file, requiring our data and file path.
    {
        string jsonData = JsonUtility.ToJson(data); // We convert our data to JSON.
        System.IO.File.WriteAllText(dataPath, jsonData); // We save our data.
    }
}

Usage of Script for Creating and Reading JSON with Unity

Now let's see how to use this script we created.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class UBJsonDataManagerExample : MonoBehaviour
{
    UBJsonDataManagerExampleSingleData singleData; // We call the class for single data. We enter the values we want through the editor.
    UBJsonDataManagerExampleWrapper multipleData; // We call the class for an array. We enter the values we want through the editor.

    UBJsonDataManagerExampleSingleData readedSingleData; // We will print the data we fetch here.
    UBJsonDataManagerExampleWrapper readedMultipleData; // We will print the array data we fetch here.

    public void SaveData() // We created a function to save the data.
    {
        string singleDataPath = Application.dataPath + "/singleData.json"; // We entered the location of our single file.
        string multipleDataPath = Application.dataPath + "/multipleData.json"; // We entered the location of the array file.

        UBJsonDataManager.SaveJSONData(singleData, singleDataPath); // We saved our single data, entered the data and file path to save.
        UBJsonDataManager.SaveJSONData(multipleData, multipleDataPath); // We saved our multiple data, entered the data and file path to save.
    }

    public void ReadData() // We create a function to read the data.
    {
        string singleDataPath = Application.dataPath + "/singleData.json"; // We entered the location of our single file.
        string multipleDataPath = Application.dataPath + "/multipleData.json"; // We entered the location of the array file.

        readedSingleData = UBJsonDataManager.ReadJSONData(singleDataPath, typeof(UBJsonDataManagerExampleSingleData)); // We fetched our single data, sent file path and data type.
        readedMultipleData = UBJsonDataManager.ReadJSONData(multipleDataPath, typeof(UBJsonDataManagerExampleWrapper)); // We fetched our multiple data, sent file path and data type.
    }

    private void Awake()
    {
        SaveData(); // We saved the data.
        ReadData(); // We fetched the data.
    }

}


[System.Serializable]
public class UBJsonDataManagerExampleWrapper // We need to create a wrapper to save array data. We are creating a wrapper.
{
    public UBJsonDataManagerExampleSingleData[] uBJsonDataManagerExampleSingleDatas; // To save multiple data, we created an array inside the new class.
}

[System.Serializable]
public class UBJsonDataManagerExampleSingleData // We are creating a class to create data, thanks to this we can edit data in the editor or fetch from a JSON file.
{
    public int exampleSingleDataInt;
    public string exampleSingleDataString;
}




Yes friends, if you have any questions about creating and reading JSON in Unity, you can ask me in the comment section below. Have a nice day.