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):
		yield(get_tree().create_timer(1.1), "timeout")
		yield(get_tree().create_timer(1.1), "timeout")
		var x =1
		if x == 1:
			var reward = "Lawnmower"
		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)


: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())
    if (json.result) == 'name: "Vino"':

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 == 'Vino':

See also the print example.

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