How to return an array within an json array.

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

I have been working on a grand strategy game in Godot as a test. I ran into trouble when trying to code the province lookup system for the map. I have made a province look up json file with each rgb value paired with a province id. I don’t seem to be able to get the code to find the rgb value and give me the province name and all of the answers i have seen don’t work well with huge files.
Province Id lookup array: Province_ID.json - Google Drive

Any help is welcome. Thank you in advance!

:bust_in_silhouette: Reply From: JimArtificer

The answer you are looking for might just be: myvar = myarray[1][0]

In your example the array you want to pull data out of is actually inside of another array. One simple debug technique you could use is to print the value you have accessed to the console.

A downside of using an array for that data is that you have to scan the array when you want to reference one of the entries. I would recommend using a Dictionary instead so that you can lookup an id much faster.

Dictionary objects are represented as objects in JSON.

You might choose to represent your data like this:

{
  "prov0": {
    "color": "(194, 193, 102, 175)",
    "population": 40200
  },
  "prov1": {
    "color": "(135, 205, 101, 93)",
    "population": 2000
  },
  ...
}

Assuming you are using GDScript, this reference should help:

Do i have to fix the whole json file by hand?

Sinowa-Programming | 2020-06-22 16:21

I would suggest starting with a small example, less than a dozen entries. Once you have the code working the way you want it to, start loading the full set of data.

You don’t need to modify your data format. It was just something to consider. If you decide to convert the format there are a variety of ways you could automate the process.

JimArtificer | 2020-06-25 02:01