Need help in choosing random questions from a .json file

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

I am new to Godot. I have a json file with several questions and want to randomly pick questions from that how can i access them.

"1":
  {
    "image": "res://Images/Stills/1.jpg",
    "question": "NIL",
    "choices": {"a":"A Wednesday!","b":"Sunday","c":"Aiyaary","d":"shoot on sight"},
    "hint": "Naseeruddin shah plays a common man determined to punish millitants on a particular day.",
    "answer": "a"
  },
  "2":
  {
    "image": "res://Images/Stills/2.jpg",
    "question": "NIL",
    "choices": {"a":"Golmaal","b":"Singham","c":"Shivaay","d":"Omkara"},
    "hint": "Ajay Devgn is a mountaineer who makes a living by providing treks and climbing expeditions to tourists.",
    "answer": "c"
  },

Here is my code

var file = File.new()
file.open("res://QuizData/BollyQuestions.json",File.READ)
var json_str = file.get_as_text()
var game_data = {}
game_data.parse_json(json_str)
var randQuestion =get_random_number()
var image = load(game_data.randQuestion.image)

func get_random_number():
    randomize()
    return randi()%3

Basically randQuestion should randomly generate “1”,“2” and so on tried str(randQuestion) didn’t work.

randi()%3 will also return 0.

jandrewlong | 2018-05-08 14:25

:bust_in_silhouette: Reply From: volzhs

game_data.randQuestion.image is actually like below.

var game_data = {
    "randQuestion" : {
        "image": "path..."
    }
}

if you want to use it as variable,
you should do like this game_data[randQuestion].image
game_data.randQuestion.image is same with game_data["randQuestion"].image

Thank you very much game_data[randQuestion].image does the trick.

CodeShaastra | 2018-05-09 06:27