Printing backend Json result in Godot

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

I have a backend setup which sends the user a random reward upon finishing the game.
Currently everything is working well however I don’t know how to read the result and display an image and label depending on what reward the user gets.
Any suggestions?
What I have currenly is:


func _on_Barrel01Area_input_event(viewport, event, shape_idx):
	if (event is InputEventMouseButton && event.pressed):
		get_tree().get_root().set_disable_input(true)
		$SmasherSprite/SmasherAttack.play("smash")
		yield(get_tree().create_timer(1.1), "timeout")
		$Barrel01.play("idle")
		yield(get_tree().create_timer(1.1), "timeout")
		#random.randomize()
		var x =1
		if x == 1:
			var reward = "Lawnmower"
		$Barrel01.hide()
		$Barrel02.hide()
		$Barrel03.hide()
		$SmasherSprite.hide()
		$SelectedRewards.show_Lawnmower()
		get_tree().get_root().set_disable_input(false)
		var url = "http://localhost:8080/endGame"
		var body = {"score": globals.score,"time": globals.time}
		var token = JavaScript.eval("localStorage.getItem('TOKEN')")
		var jsonBody = JSON.print(body)
		$HTTPRequest.request(url, ["Authorization: %s" % token, "Content-length: %d" % jsonBody.length(), "Content-type: application/json"], false, HTTPClient.METHOD_POST, jsonBody)
		#$HTTPRequest.request(url, [], true, HTTPClient.METHOD_POST)

And

:bust_in_silhouette: Reply From: clemens.tolboom

You have to follow Making HTTP requests — Godot Engine (stable) documentation in English in connecting your HTTPRequest and proces there the received JSON.

The examples in HTTPRequest — Godot Engine (stable) documentation in English may help too.

I’ve managed to get it to work however a thing I have no clue how to do is json matching, so if my data is equal to lets say {name: Vino}. How would I make something happen if the Json result is exactly Vino?
I’ve tried this but it does not work, tried different ways to write the name part such as {name: Vino} etc.

	var json = JSON.parse(body.get_string_from_utf8())
    print(json.result)
    if (json.result) == 'name: "Vino"':
	$SelectedRewards/Lawnmower.show()
	$SelectedRewards/LabelLawnmower.show()

jebenipapa | 2021-08-03 10:01

The result of var json = JSON.parse(body.get_string_from_utf8()) is a JSONParseResult

Reading that page you should (assuming it is a Dictionary as it can also be an Array)

if json_result.error == OK:
  var my_data :Dictionary = json.result
  if my_data.name == 'Vino':
    $SelectedRewards/Lawnmower.show()
    $SelectedRewards/LabelLawnmower.show()

See also the print example.

clemens.tolboom | 2021-08-05 14:43