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: October 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.
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.
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.
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:
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.
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.
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:
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:
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.