Need Help With JSON Or XML Data Extraction for Bible app in Godot 4

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

I am currently working on a Bible reader in Godot 4 where I need to extract information from a JSON file (or XML file) to be displayed. But I have little to no knowledge about working with JSON or XML in Godot, and to make things worse there is no documentation or tutorial for what i want to do. I am hoping someone here could help me with my problem.

Here is what the structure of the JSON (and XML) file is like:

{ "chapter": 1, "verse": 1, "text": "In the beginning God created the heavens and the earth.", "translation_id": "ASV", "book_id": "Gen", "book_name": "Genesis" } { "chapter": 1, "verse": 2, "text": "And the earth was waste and void; and darkness was upon the face of the deep: and the Spirit of God moved upon the face of the waters", "translation_id": "ASV", "book_id": "Gen", "book_name": "Genesis" } { "chapter": 1, "verse": 3, "text": "And God said, Let there be light: and there was light.", "translation_id": "ASV", "book_id": "Gen", "book_name": "Genesis" }

What I want to do is to extract the ‘text’ data based on the ‘chapter’ and ‘verse’ that I specify. For example, if I ask for chapter 1, verse 2, I should get the corresponding text in a variable to then be displayed on a label.

Any help on how I can achieve this?

Here’s my source for the Bible text.

:bust_in_silhouette: Reply From: HyperlinkYourHeart

There is a JSON parser class which is documented here. If you include JSON files in your project they will automatically be imported like any other resource, with the parsed data available on the data property.

The JSON in that GitHub does not appear to be well formed to import as-is, so you would need to adapt it before it can be imported. The problem is that it’s a bunch of loose object thrown into a file without any enclosing structure. You will need to wrap it in an array, and add commas at the end of every line.

With those changes made it should import and you can access it like this:

const json_resource = preload("res://kjv.json")

func _ready():
    # Shows "In the beginning..." in a label
    $Label.text =[0].text

If you want to reference it by chapter and verse you will probably want to munge the data into a dictionary of dictionaries with chapter and verse keys so you don’t have to loop over the entire array every time you need to look for a specific verse.