Load a JSON file to a variable

You can load the content of a local JSON file into a variable.

For native games, you can only load a JSON file from a website. See the note at the end of this page.

First, you need a json file that you'll put next to the index.html file when your game is exported. For example:

    "title": "My great news",
    "content": "Hello everybody"

In your game, add an action called “Send a HTTP request to a website”.

  • For the host, enter an empty string: “” (as we're using a local file put in the game folder). Do not forget the quotes!
  • For the path, enter the name of the JSON file: “news.json” (Do not forget the quotes again!)
  • For the body of the request, enter “” again.
  • For the last parameter, enter the name of the variable that will be filled with the JSON file. For example, RawNews.

Add a Text object, and set the text of this object to the value of the RawNews variable. Put the object on the scene. Export your game, put the news.json file next to the index.html, send the game to a website and open it with a browser. You'll see the variable filled with the JSON.

This won't work if you're not sending your game to a website.

Ok, great, we can now display the content of the JSON file! How to read the value of the title and content.

Add an action after the first one. Choose action Convert JSON to variable.

  • For the first parameter (“JSON string”), the action expect the content of the json, so enter VariableString(RawNews). (As the RawNews variable contains the JSON)
  • Then enter the name of the variable that will contain the value of the JSON. For example, News.

That's all! Now you can access to the title like this VariableString(News.title) (and the content in the same way: VariableString(News.content)).

For example, you can add a text object and this action:

Do =VariableString(News.content) to the text of object MyTextObject

Again, export the game, put it on a website, put the json file next to the index.html.
The object called MyTextObject will now display Hello everybody! You can update the json file and relaunch the game, and the text will be updated! This is great to make a news system.

Here we are loading a local JSON file. It works only for HTML5 games.
For native games, you have to enter the website name in the “host” parameter. For example, if your JSON file is hosted at http://www.example.com/test/news.json, enter http://www.example.com for the name of the host, and “test/news.json” for the name of the page. It will works for native games and HTML5 games (For HTML5 games, just be sure to host your game on the same domain, www.example.com here).