Writing JSON Data in SplashKit
Having covered how to read JSON data in SplashKit, this part of the tutorial focuses on creating and writing data to JSON files. This functionality is crucial for features like saving game settings or player progress and information.
Written by: Jonathan Tynan and others
Last updated: December 2024
Creating JSON Objects and Arrays
In SplashKit, you can programmatically create JSON objects and arrays, which then can be populated with data. Lets see how we can create the example JSON file from previous tutorials with this method.
json new_game_data = create_json();json_set_string(new_game_data, "gameTitle", "My New Game");json_set_bool(new_game_data, "fullScreenMode", false);json_set_number(new_game_data, "numPlayers", 1);
Json newGameData = CreateJson();JsonSetString(newGameData, "gameTitle", "My New Game");JsonSetBool(newGameData, "fullScreenMode", false);JsonSetNumber(newGameData, "numPlayers", 1);
Json newGameData = SplashKit.CreateJson();SplashKit.JsonSetString(newGameData, "gameTitle", "My New Game");SplashKit.JsonSetBool(newGameData, "fullScreenMode", false);SplashKit.JsonSetNumber(newGameData, "numPlayers", 1);
new_game_data = create_json()
json_set_string(new_game_data, "gameTitle", "My New Game")json_set_bool(new_game_data, "fullScreenMode", False)json_set_number_integer(new_game_data, "numPlayers", 1)
First we create the new JSON object using Create Json, then we add basic data to gameTitle, fullScreenMode, and numPlayers using Json Set String, Json Set Bool and Json Set Number.
vector<string> levels_array;
levels_array.push_back("level1");levels_array.push_back("level2");levels_array.push_back("level3");
json_set_array(new_game_data, "levels", levels_array);
List<string> levelsArray = new List<string>{ "level1", "level2", "level3"};
JsonSetArray(newGameData, "levels", levelsArray);
List<string> levelsArray = new List<string>{ "level1", "level2", "level3"};
SplashKit.JsonSetArray(newGameData, "levels", levelsArray);
levels_array = ["level1", "level2", "level3"]json_set_array_of_string(new_game_data, "levels", levels_array)
Next we add the levels array to the JSON object. We create a vector to store the strings, and push back each string that we want. Finally we use Json Set Array to store this data in JSON format.
json screen_size_data = create_json();
json_set_number(screen_size_data, "width", 800);json_set_number(screen_size_data, "height", 600);
json_set_object(new_game_data, "screenSize", screen_size_data);
Json screenSizeData = CreateJson();
JsonSetNumber(screenSizeData, "width", 800);JsonSetNumber(screenSizeData, "height", 600);
JsonSetObject(newGameData, "screenSize", screenSizeData);
Json screenSizeData = SplashKit.CreateJson();
SplashKit.JsonSetNumber(screenSizeData, "width", 800);SplashKit.JsonSetNumber(screenSizeData, "height", 600);
SplashKit.JsonSetObject(newGameData, "screenSize", screenSizeData);
screen_size_data = create_json()
json_set_number_integer(screen_size_data, "width", 800)json_set_number_integer(screen_size_data, "height", 600)
json_set_object(new_game_data, "screenSize", screen_size_data)
Then we tackle the nested JSON object, the screen size object. We use Create Json to create a new object for this data, and then we add the width and the height to the object using Json Set Number. Now that we have this JSON object filled with the data we want, we add it to the new_game_data
object with Json Set Object.
Writing JSON Data to a File
Now that we have the new_game_data
object that stores the same values as the JSON file used previously. Now, we can save this using Json To File like in the code below.
json_to_file(new_game_data, "new_game_data.json");
JsonToFile(newGameData, "new_game_data.json");
SplashKit.JsonToFile(newGameData, "new_game_data.json");
json_to_file(new_game_data, "new_game_data.json")
By combining all these examples we can create the full program shown below.
#include "splashkit.h"
int main(){ json new_game_data = create_json();
json_set_string(new_game_data, "gameTitle", "My New Game"); json_set_bool(new_game_data, "fullScreenMode", false); json_set_number(new_game_data, "numPlayers", 1);
json screen_size_data = create_json();
json_set_number(screen_size_data, "width", 800); json_set_number(screen_size_data, "height", 600);
json_set_object(new_game_data, "screenSize", screen_size_data);
vector<string> levels_array;
levels_array.push_back("level1"); levels_array.push_back("level2"); levels_array.push_back("level3");
json_set_array(new_game_data, "levels", levels_array);
json_to_file(new_game_data, "new_game_data.json");
free_all_json();}
using SplashKitSDK;using static SplashKitSDK.SplashKit;using System.Collections.Generic;
Json newGameData = CreateJson();
JsonSetString(newGameData, "gameTitle", "My New Game");JsonSetBool(newGameData, "fullScreenMode", false);JsonSetNumber(newGameData, "numPlayers", 1);
Json screenSizeData = CreateJson();JsonSetNumber(screenSizeData, "width", 800);JsonSetNumber(screenSizeData, "height", 600);
JsonSetObject(newGameData, "screenSize", screenSizeData);
List<string> levelsArray = new List<string>{ "level1", "level2", "level3"};
JsonSetArray(newGameData, "levels", levelsArray);
JsonToFile(newGameData, "new_game_data.json");FreeJson(newGameData);
FreeJson(screenSizeData);
using SplashKitSDK;using System.Collections.Generic;
namespace WritingJsonData{ public class Program { public static void Main() { Json newGameData = SplashKit.CreateJson();
SplashKit.JsonSetString(newGameData, "gameTitle", "My New Game"); SplashKit.JsonSetBool(newGameData, "fullScreenMode", false); SplashKit.JsonSetNumber(newGameData, "numPlayers", 1);
Json screenSizeData = SplashKit.CreateJson(); SplashKit.JsonSetNumber(screenSizeData, "width", 800); SplashKit.JsonSetNumber(screenSizeData, "height", 600);
SplashKit.JsonSetObject(newGameData, "screenSize", screenSizeData);
List<string> levelsArray = new List<string> { "level1", "level2", "level3" };
SplashKit.JsonSetArray(newGameData, "levels", levelsArray);
SplashKit.JsonToFile(newGameData, "new_game_data.json"); SplashKit.FreeJson(newGameData);
SplashKit.FreeJson(screenSizeData); } }}
from splashkit import *
new_game_data = create_json()
json_set_string(new_game_data, "gameTitle", "My New Game")json_set_bool(new_game_data, "fullScreenMode", False)json_set_number_integer(new_game_data, "numPlayers", 1)
screen_size_data = create_json()json_set_number_integer(screen_size_data, "width", 800)json_set_number_integer(screen_size_data, "height", 600)
json_set_object(new_game_data, "screenSize", screen_size_data)
levels_array = ["level1", "level2", "level3"]
json_set_array_of_string(new_game_data, "levels", levels_array)json_to_file(new_game_data, "new_game_data.json")
free_all_json()
Running this program results in a file named new_game_data.json
being written to the Resources/json/
folder. Open this up and you’ll see something very similar or identical to the example JSON file we’ve been using previously. It should look something like this:
{ "numPlayers": 1, "fullScreenMode": false, "gameTitle": "My New Game", "levels": [ "level1", "level2", "level3" ], "screenSize": { "height": 600, "width": 800 }}
Some of the keys can be in different positions, but this does not affect how we use it as we look for the key when retrieving values, not a particular data position in the JSON file. This new file is effectively the same JSON that we’ve used in previous JSON tutorials.
Modifying Existing JSON Data
You can also load an existing JSON file, modify its contents, and save the changes back to the file. To demonstrate this, lets add the details of a player character to our game data.
json player_data = create_json();json_set_string(player_data, "name", "Hero");
json stats_data = create_json();json_set_number(stats_data, "health", 100);json_set_number(stats_data, "mana", 50);json_set_number(stats_data, "strength", 75);
json_set_object(player_data, "stats", stats_data);
Json playerData = CreateJson();JsonSetString(playerData, "name", "Hero");
Json statsData = CreateJson();JsonSetNumber(statsData, "health", 100);JsonSetNumber(statsData, "mana", 50);JsonSetNumber(statsData, "strength", 75);
JsonSetObject(playerData, "stats", statsData);
Json playerData = SplashKit.CreateJson();SplashKit.JsonSetString(playerData, "name", "Hero");
Json statsData = SplashKit.CreateJson();SplashKit.JsonSetNumber(statsData, "health", 100);SplashKit.JsonSetNumber(statsData, "mana", 50);SplashKit.JsonSetNumber(statsData, "strength", 75);
SplashKit.JsonSetObject(playerData, "stats", statsData);
player_data = create_json()json_set_string(player_data, "name", "Hero")
stats_data = create_json()json_set_number_integer(stats_data, "health", 100)json_set_number_integer(stats_data, "mana", 50)json_set_number_integer(stats_data, "strength", 75)
json_set_object(player_data, "stats", stats_data)
First we create the player JSON object to store the data for an entire character, then we create an individual object to hold the stats for the character. After this we add the stats object and nest it in the player_data
object we created earlier.
json existing_data = json_from_file("new_game_data.json")json_set_object(existing_data, "character", player_data);
json_to_file(existing_data, "modified_game_data.json");
Json existingData = JsonFromFile("new_game_data.json");JsonSetObject(existingData, "character", playerData);
JsonToFile(existingData, "modified_game_data.json");
Json existingData = SplashKit.JsonFromFile("new_game_data.json");SplashKit.JsonSetObject(existingData, "character", playerData);
SplashKit.JsonToFile(existingData, "modified_game_data.json");
existing_data = json_from_file("new_game_data.json")json_set_object(existing_data, "character", player_data)
json_to_file(existing_data, "modified_game_data.json")
Next we load the game data we saved previously, add our player_data
object to the existing data and save it. If we add this code to our previous program and run it a file is created in the Resources/json/
folder named modified_game_data.json
. Open it, and you should see something like the following:
{ "character": { "name": "Hero", "stats": { "health": 100, "mana": 50, "strength": 75 } }, "fullScreenMode": false, "numPlayers": 1, "gameTitle": "My New Game", "levels": [ "levels1", "levels2", "levels3" ], "screenSize": { "height": 600, "width": 800 }}
Now we have a character object stored with this JSON file. We also now have multiple levels of nesting. When this is the case and we want to access the innermost key we must get these JSON objects. So, to access the health stat we can use the following code:
// Load our JSONjson modified_game_data = json_from_file("modified_game_data.json");// Retrieve Character JSON object from the file.json character = json_read_object(modified_game_data, "character");// Retrieve the Stats JSON object from the Character JSONjson stats = json_read_object(character, "stats");// Retrieve the value of health from the stats JSON objectint hp = json_read_number_as_int(stats, "health");
// Load our JSONJson modifiedGameData = JsonFromFile("modified_game_data.json");
// Retrieve Character JSON object from the file.Json modifiedGameData = JsonFromFile("modified_game_data.json");
// Retrieve the Stats JSON object from the Character JSONJson stats = JsonReadObject(character, "stats");
// Retrieve the value of health from the stats JSON objectint hp = JsonReadNumberAsInt(stats, "health");
// Load our JSONJson modifiedGameData = SplashKit.JsonFromFile("modified_game_data.json");
// Retrieve Character JSON object from the file.Json character = SplashKit.JsonReadObject(modifiedGameData, "character");
// Retrieve the Stats JSON object from the Character JSONJson stats = SplashKit.JsonReadObject(character, "stats");
// Retrieve the value of health from the stats JSON objectint hp = SplashKit.JsonReadNumberAsInt(stats, "health");
# Load our JSONmodified_game_data = json_from_file("modified_game_data.json")
# Retrieve Character JSON object from the file.character = json_read_object(modified_game_data, "character")
# Retrieve the Stats JSON object from the Character JSONstats = json_read_object(character, "stats")
# Retrieve the value of health from the stats JSON objecthp = json_read_number_as_int(stats, "health")
Conclusion
By following this tutorial, you’re now equipped with the foundational skills necessary to create, read and write JSON data objects with SplashKit. These examples have been focused around game development, but the JSON skills you’ve learnt extends beyond this as JSON is a versatile tool for any software development project.