How to handle Json files

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

so I have the following script wich downloads a json file from an api:

  extends Node

@onready var Http = $Request
@onready var dir = "res://Data/Stage/"
@export var StageURL = ""
var file_name = "GlobalStage.json"
var token = "token: *******************************"
var path = 'res://Data/Stage/GlobalStage.json'
@export var headers = PackedStringArray([token])
@export_multiline var request_data: String

func _ready():

func Request():
	$Request.request("", headers)

func Return(result: int, response_code: int, headers: PackedStringArray, body: PackedByteArray):
	var status = JSON.parse_string(body.get_string_from_utf8())
	download_file(StageURL, path)
	print("shcedule: \n", status)
func download_file(StageURL, path):
	$Download.request(StageURL, headers)
	print("file Downloaded")

now when I open the json file in the godot editor all the information in it is on one line

when I open that same file in firefox it is formatted correctly. so I would like to know how to have it not read the file as if everything was on one line and display it in the correct format. I am opening the file in the editor and not reading it through a script.

:bust_in_silhouette: Reply From: jgodfrey

A few things. First, a single line of JSON is perfectly valid and more compact for sending over the wire. That said, it’s certainly not as easy to ready manually.

Further, Firefox has an in-built JSON viewer that’s likely taking your file (which I assume IS a single line of text) and formatting it for easier manual reading.

Bottom line. The Godot editor is showing you the ACTUAL file, while Firefox is preprocessing it before showing it to you.

If you want to format the JSON, the JSON.stringify() function has some abilities here via its indent argument…

ok thank you for clarifying the part where it is actually one line.
next I would like to know how would I go about reading a specific section of the file? and then outputting it to a label?

Cyber-Kun | 2023-03-04 21:15

Assuming you successfully parse the JSON data into a valid Godot datatype (likely a Dictionary) via JSON.parse() or JSON.parse_string(), you can access any part of the data using standard Dictionary syntax. Check out the docs here:

C# collections — Godot Engine (latest) documentation in English

jgodfrey | 2023-03-04 21:22

I followed the link you gave me above and it goes to a c# section is there a way to do it in GDScript or do I have to learn C# for that?

also I thank you so much for your help so far

Cyber-Kun | 2023-03-04 21:25

Ah, sorry about that. I did link to the C# docs. Here’s the correct link for GDScript:

Dictionary — Godot Engine (latest) documentation in English

The docs are pretty good, so you should get used to searching / navigating / using them.

jgodfrey | 2023-03-04 21:45

thank you very much I will read through the docs

Cyber-Kun | 2023-03-05 07:39