[TUTORIAL] Read data from an external .JSON file

DISCLAIMER: This is the way I do it, maybe you can do in another way, mine works so if you want something that work, just follow along. Also please be careful with your project file, copy it everywhere before doing this, I won’t be responsible for any data loss.

First you need to understand how a .JSON file is structured, it is very important since otherwise it just won’t work without any warning.

You open any notepad, save your file with the .json extension and structure it like this:

[{
“initialisation” : {
“level” : “1”,
“life” : “100”,
“time” : “60”
},
“monsters” : {
“monster0” : “100”,
“monster1” : “90”,
“monster2” : “200”,
“monster3” : “60”
},
“friendsname” : {
“friend0” : “Mark”,
“friend1” : “Justin”,
“friend2” : “Robert”,
“friend3” : “Brad”
}
}]

VERY IMPORTANT: the comas play the major role here, let me dig into it:

[{ (we open the main structure (this will be 0))
“name of the first tree” : { (we open it)
“name of the first value of the first tree” : “value”, <–there is a coma
“name of the second value of the first tree” : “other value” <—no coma here since I don’t have more value in the first tree
}, (I close the first tree, add a coma since I have more trees to write down)
“name of the 2nd tree” : { (we open the second tree)
“name of the first value of the 2nd tree” : “value”, ← there is a coma
“name of the 2nd value of the 2nd tree” : “value”, ← there is a coma
“name of the third value of the 2nd tree” : “value” ← no coma since I’m closing the 2nd tree
} (I close the second tree, no coma since I have finished the main structure (0)
}] (the main structure is closed)

Alright, now we’re gonna have to declare this JSON file inside your Gdevelop project since (for the moment) you can’t import a .JSON file as a resource directly inside Gdevelop.
It’s a little hacky, so save a copy of your project before doing this.

Open your main Gdevelop project with any notepad, and wait! It’s a .json file too!!
That means it’s structured exactly like your file, so keep that in mind.

Inside your project you will find a tree called “resources”
Here is listed all your pictures and sound used inside your Gdevelop project.
Basically a resource is written like this:

  {
    "alwaysLoaded": false,
    "file": "Background.jpg",
    "kind": "image",
    "metadata": "",
    "name": "Background.jpg",
    "smoothed": false,
    "userAdded": true
  }, <-- here, there is a coma since it is **NOT** the last resource declared!!

You want to declare your JSON file between two pictures just to not get confused or forget this damned coma.
Let’s say this will go like this:

“resources”: {
“resources”: [
{
“alwaysLoaded”: false,
“file”: “Background.jpg”,
“kind”: “image”,
“metadata”: “”,
“name”: “Background.jpg”,
“smoothed”: false,
“userAdded”: true
},
MAKE SPACE MY JSON WILL BE HERE!
{
“alwaysLoaded”: false,
“file”: “NewObject-1-0.png”,
“kind”: “image”,
“metadata”: “”,
“name”: “NewObject-1-0.png”,
“smoothed”: false,
“userAdded”: true
},

I usually declare the JSON file like a picture so it goes like this:

 {
    "disablePreload": false,
    "file": "myfile.json", <-----the name of your file with the json extension
    "kind": "json",
    "metadata": "",
    "name": "myfile", <----------the name of your file without the json extension
    "userAdded": true
  }, <---------------------------there IS a COMA!

So now my Gdevelop game project looks like this:

“resources”: {
“resources”: [
{
“alwaysLoaded”: false,
“file”: “Background.jpg”,
“kind”: “image”,
“metadata”: “”,
“name”: “Background.jpg”,
“smoothed”: false,
“userAdded”: true
},
{
“disablePreload”: false,
“file”: “myfile.json”,
“kind”: “json”,
“metadata”: “”,
“name”: “myfile”,
“userAdded”: true
},
{
“alwaysLoaded”: false,
“file”: “NewObject-1-0.png”,
“kind”: “image”,
“metadata”: “”,
“name”: “NewObject-1-0.png”,
“smoothed”: false,
“userAdded”: true
},

Save your file and now open your project with Gdevelop.
In “game settings”, open the Resources panel and you will see your file declared here!

Now it’s time to USE IT!

You want to declare it At the begining of the scene to use it later.
Add an action “Send a request to a web page” this will read the whole file and store it inside a variable.
Host, with protocol: “”
Path:myfile.json
Request body content: “”
Method: “GET”
Content type stays Content type
Reponse scene variable: myjsonfilevariable

This is great but this stores the whole file inside a big string, this doesn’t really help us, so now we want to parse it.
After this you want to add a “Parse a JSON object and store it into a global variable”. This will do as it says, meaning everything will now be usable:
JSON string: VariableString(myjsonfilevariable) <–this gets the big string I just created with the request above
Global variable where store the JSON object:
myjsonparsed

Now I have a global variable, this means I can get it at any time in any scene.

Let’s say I want to get the information stored inside “initialisation”>“life” from my file.
I need to call the global string and structure it:
GlobalVariableString(myjsonparsed.0.initialisation.life)

Let me explain it:
GlobalVariableString --this calls a string from a global variable
myjsonparsed is the name of the Global Variable
0 is my main structure
initialisation and life is the rest of the path tree.
This will return this string : “100”

If you need it to be a number just call this:
ToNumber(GlobalVariableString(myjsonparsed.0.initialisation.life))
And this will return this number: 100

BONUS:
A structure variable is made so that you can either call it directly or call it dynamically.
Let’s say I want to randomize the name of a friend I meet.
I will call:
GlobalVariableString(myjsonparsed.0.friendsname[(“friend”+ToString(Random(3)))])

The [ opens up the friendsname tree and from there I can randomize a number up to 3 (Random starts at 0 up to the number you declare)
and stick it to the “friend” tree
It can return something like this:
GlobalVariableString(myjsonparsed.0.friendsname[“friend2”]) = “Robert”

There you have it! Have fun!!

16 Likes

Wow, you it helps me a lot. Thanks

Huge thanks! It helped a lot

1 Like

Thanks a lot for this explanation.
I used it a bit differently and it might be helpful for me.
I created a Json txt file on my desktop :

{
“namem” : {
“1” : “John”,
“2” : “Robert”,
},
“namef” : {
“1” : “Linda”,
“2” : “Cindy”,
}
}

Then I use the action load the text from a file into a scene variable.
And then I parse Json string of this scene variable and store it into variable “name”.

So then I have my name variable made. For example, name.namef.1 will be “Linda”.

It can be handy to deal with variables in txt files without having to modify the main gdevelop project file.

Thanks for this!

The loading technique didn’t work for me but this JavaScript works (I’m putting story data into “myfile” and storing it in a global variable called “MyVariable”, here) – just click “Add…” and choose “Javascript code”:

const dataVar = runtimeScene.getGame().getVariables().get("MyVariable")

if (dataVar.getChildrenCount() === 0) {
    dataVar.fromJSObject(runtimeScene.getGame().getJsonManager().getLoadedJson("myfile"))
}