Reading JSON Data in SplashKit
After understanding the basics of JSON in SplashKit, this part of the tutorial focuses on how to read and parse JSON data. Reading JSON data is essential for game development tasks such as loading game settings, level configurations, or player data.
Written by: Jonathan Tynan and others
Last updated: December 2024
Reading JSON Objects
In the previous tutorial we loaded the following JSON file and read the game title from it. Lets extend this a little, and dive a further into extracting values from this structure.
{ "gameTitle": "My New Game", "fullScreenMode": false, "numPlayers": 1, "screenSize": { "width": 800, "height": 600 }, "levels": ["level1", "level2", "level3"]}
Accessing Values
To access values in JSON objects like strings, numbers, or booleans, you can use functions like Json Read String, Json Read Number As Int, or Json Read Bool. We use these functions like the following code snippet.
json game_data = json_from_file("game_data.json");
string title = json_read_string(game_data, "gameTitle");int numPlayers = json_read_number_as_int(game_data, "numPlayers");bool isFullScreen = json_read_bool(game_data, "fullScreenMode");
Json gameData = JsonFromFile("game_data.json");
string title = JsonReadString(gameData, "gameTitle");int numPlayers = JsonReadNumberAsInt(gameData, "numPlayers");bool isFullScreen = JsonReadBool(gameData, "fullScreenMode");
Json gameData = SplashKit.JsonFromFile("game_data.json");
string title = SplashKit.JsonReadString(gameData, "gameTitle");int numPlayers = SplashKit.JsonReadNumberAsInt(gameData, "numPlayers");bool isFullScreen = SplashKit.JsonReadBool(gameData, "fullScreenMode");
game_data = json_from_file("game_data.json")
title = json_read_string(game_data, "gameTitle")num_players = json_read_number_as_int(game_data, "numPlayers")is_full_screen = json_read_bool(game_data, "fullScreenMode")
Working with JSON Arrays
If the data is an array, like the value stored for the levels
key, we can obtain the data through Json Read Array and store it into a dynamic string array variable (such as vector<string>
in C++, or List<string>
in C#). Then we can loop through the entries in the array, and do some actions with the stored data.
Below is an example of this:
vector<string> levels;
json_read_array(game_data, "levels", levels);
int num_levels = levels.size();
for(int i = 0; i < num_levels; i++){ write("Got level: "); write_line(levels[i]);}
List<string> levels = new List<string>();
JsonReadArray(gameData, "levels", ref levels);
int numLevels = levels.Count;
for (int i = 0; i < numLevels; i++){ WriteLine($"Got level: {levels[i]}");}
List<string> levels = new List<string>();
SplashKit.JsonReadArray(gameData, "levels", ref levels);
int numLevels = levels.Count;
for (int i = 0; i < numLevels; i++){ SplashKit.WriteLine($"Got level: {levels[i]}");}
levels = []
json_read_array_of_string(game_data, "levels", levels)
num_levels = len(levels)
for i in range(num_levels): write_line(f"Got level: {levels[i]}")
Running this prints the following to the terminal:
Got level: level1Got level: level2Got level: level3
Extracting Nested JSON Objects
SplashKit’s JSON functionality allows you to extract various types of data, including basic types mentioned previously, but also even nested JSON objects. In our example file the value for the screenSize
key is a JSON object. The following code demonstrates how to extract this object:
json game_screen_size = json_read_object(game_data, "screenSize");int width = json_read_number_as_int(game_screen_size, "width");int height = json_read_number_as_int(game_screen_size, "height");
write_line("Screen Width: " + to_string(width));write_line("Screen Height: " + to_string(height));
Json gameScreenSize = JsonReadObject(gamedata, "screenSize");int width = JsonReadNumberAsInt(gameScreenSize, "width");int height = JsonReadNumberAsInt(gameScreenSize, "height");
WriteLine($"Screen Width: {width}");WriteLine($"Screen Height: {height}");
Json gameScreenSize = SplashKit.JsonReadObject(gamedata, "screenSize");int width = SplashKit.JsonReadNumberAsInt(gameScreenSize, "width");int height = SplashKit.JsonReadNumberAsInt(gameScreenSize, "height");
SplashKit.WriteLine($"Screen Width: {width}");SplashKit.WriteLine($"Screen Height: {height}");
game_screen_size = json_read_object(game_data, "screenSize")width = json_read_number_as_int(game_screen_size, "width")height = json_read_number_as_int(game_screen_size, "height")
write_line(f"Screen Width: {width}")write_line(f"Screen Height: {height}")
Running this prints the following to the terminal:
Screen Width: 800Screen Height: 600
Putting it all together
By combining all these examples we can create the full program shown below.
#include "splashkit.h"using namespace std;
int main(){ // Load the game data JSON file json game_data = json_from_file("game_data.json");
// Read the game data from the JSON string title = json_read_string(game_data, "gameTitle"); int numPlayers = json_read_number_as_int(game_data, "numPlayers"); bool isFullScreen = json_read_bool(game_data, "fullScreenMode"); vector<string> levels;
// Write the game data to the terminal write_line("Game Title: " + title); write_line("Number of Players: " + to_string(numPlayers)); write_line("Full Screen Mode: " + to_string(isFullScreen));
// Read the levels array from the JSON json_read_array(game_data, "levels", levels);
int num_levels = levels.size();
for (int i = 0; i < num_levels; i++) { write("Got level: "); write_line(levels[i]); }
// Extract the nested JSON objects json game_screen_size = json_read_object(game_data, "screenSize"); int width = json_read_number_as_int(game_screen_size, "width"); int height = json_read_number_as_int(game_screen_size, "height");
// Write the screen size to the terminal write_line("Screen Width: " + to_string(width)); write_line("Screen Height: " + to_string(height));}
using SplashKitSDK;using static SplashKitSDK.SplashKit;
// Load the game data JSON fileJson gameData = JsonFromFile("game_data.json");
// Read the game data from the JSONstring title = JsonReadString(gameData, "gameTitle");int numPlayers = JsonReadNumberAsInt(gameData, "numPlayers");bool isFullScreen = JsonReadBool(gameData, "fullScreenMode");List<string> levels = new List<string>();
// Write the game data to the terminalWriteLine($"Game Title: {title}");WriteLine($"Number of Players: {numPlayers}");WriteLine($"Full Screen Mode: {isFullScreen}");
// Read the levels array from the JSONJsonReadArray(gameData, "levels", ref levels);
int numLevels = levels.Count;
for (int i = 0; i < numLevels; i++){ WriteLine($"Got level: {levels[i]}");}
// Extract the nested JSON objectsJson gameScreenSize = JsonReadObject(gameData, "screenSize");int width = JsonReadNumberAsInt(gameScreenSize, "width");int height = JsonReadNumberAsInt(gameScreenSize, "height");
// Write the screen size to the terminalWriteLine($"Screen Width: {width}");WriteLine($"Screen Height: {height}");
using System;using System.Collections.Generic;using SplashKitSDK;
class Program{ static void Main() { // Load the game data JSON file Json gameData = SplashKit.JsonFromFile("game_data.json");
// Read the game data from the JSON string title = SplashKit.JsonReadString(gameData, "gameTitle"); int numPlayers = SplashKit.JsonReadNumberAsInt(gameData, "numPlayers"); bool isFullScreen = SplashKit.JsonReadBool(gameData, "fullScreenMode"); List<string> levels = new List<string>();
// Write the game data to the terminal SplashKit.WriteLine($"Game Title: {title}"); SplashKit.WriteLine($"Number of Players: {numPlayers}"); SplashKit.WriteLine($"Full Screen Mode: {isFullScreen}");
// Read the levels array from the JSON SplashKit.JsonReadArray(gameData, "levels", ref levels);
int numLevels = levels.Count;
for (int i = 0; i < numLevels; i++) { SplashKit.WriteLine($"Got level: {levels[i]}"); }
// Extract the nested JSON objects Json gameScreenSize = SplashKit.JsonReadObject(gameData, "screenSize"); int width = SplashKit.JsonReadNumberAsInt(gameScreenSize, "width"); int height = SplashKit.JsonReadNumberAsInt(gameScreenSize, "height");
// Write the screen size to the terminal SplashKit.WriteLine($"Screen Width: {width}"); SplashKit.WriteLine($"Screen Height: {height}"); }}
from splashkit import *
# Load the game data JSON filegame_data = json_from_file("game_data.json")
# Read the game data from the JSON objecttitle = json_read_string(game_data, "gameTitle")num_players = json_read_number_as_int(game_data, "numPlayers")is_full_screen = json_read_bool(game_data, "fullScreenMode")levels = []
# Write the game data to the terminalwrite_line(f"Game Title: {title}")write_line(f"Number of Players: {num_players}")write_line(f"Full Screen Mode: {is_full_screen}")
# Read the levels array from the JSON filejson_read_array_of_string(game_data, "levels", levels)
num_levels = len(levels)
for i in range(num_levels): write_line(f"Got level: {levels[i]}")
# Extract the nested JSON objectsgame_screen_size = json_read_object(game_data, "screenSize")width = json_read_number_as_int(game_screen_size, "width")height = json_read_number_as_int(game_screen_size, "height")
# Write the screen size to the terminalwrite_line(f"Screen Width: {width}")write_line(f"Screen Height: {height}")
In this example, Json Read Object is used to extract the nested JSON object, and then the values are read from this nested object. These variables can then be used to define the window size for this game.
Conclusion
Reading JSON data with SplashKit is a straightforward process that can greatly enhance the flexibility and functionality of your game. It enables dynamic loading of game content and configurations, making your game more adaptable and easier to manage.
In the next part of this tutorial, we explore how to write and modify JSON data, allowing you to save game states, configurations, and player preferences.