Representing data in JSON and equivalent in GDScript

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By rogerdv

I need to store the following information:

Question
Topic
Answer 1, correct/incorrect


Answer 4, correct/incorrect

Whats the proper representation of this in JSON? I know how to do it in XML, but JSON is new for me. When I parse this into GDScript, what would be the resulting dictionary for each question (I must have a hundreth questions or so)?

:bust_in_silhouette: Reply From: Ram

I am not sure if you are trying to get data into your game/application or if you are trying to get data out (in other words are you trying to use JSON to save or load data).

There is a nice tutorial in the docs for saving and loading data using JSON.

Say I have a json :

{"Question 1":{
    "Question" : "Question text",
    "Answers":["answer 1","answer 2","answer 3","answer 4"],
    "Correct":1,
    "Topic":"Topic of question 1"
  },
"Question 2":{
    "Question" : "Question text",
    "Answers":["answer 1","answer 2","answer 3","answer 4"],
    "Correct":4,
    "Topic":"Topic of question 2"
 }
}

If you something like var dictionary_data = parse_json(whatEverJSONdata) you will get a dictionary and it has the same structure as json really.

Usefull functions parse_json and to_json.

Thats the point, I dont know how the resulting dictionary is. What I need is to keep a big database of questions (in JSON), a random question is displayed at certain points of the game.
Also, would be very convenient for me if the 4 answers are stored in an array, so I can randomize them, thus they are never displayed in the same order. As an example, the structure looked like this in C#:

class Question {
public:
string question
string topic
string answers
int correct
}

rogerdv | 2020-03-04 20:14

I dont know how the resulting dictionary is

Best way I know is to print and see for yourself how it looks like

print(whateverJSONstring)
var dict = parse_json(whateverJSONstring)
print(dict)

4 answers are stored in an array

I edited my answer to show the syntax. Just google json syntax if to understand more. There is an enormous amount of content available.

Ram | 2020-03-05 01:38