Data layout in Json

Hey I am trying to make a database work for my users data,

In json I have my array layout like this

Summary
{
    "9417418": {
        "kills": "0",
        "deaths": "0",
        "xp": "0",
        "credits": "0",
        "dz_credits": "0",
        "equipped": {
            "weapons": {
                "Vintorez": {
                    "attachments": [],
                    "skins": []
                },
                "HoneyBadger": {
                    "attachments": [
                        "ACOG"
                    ],
                    "skins": []
                },
                "ff": {
                    "attachments": [],
                    "skins": []
                }
            },
            "armour": {
                "Headgear": {
                    "skins": []
                },
                "TorsoGear": {
                    "skins": []
                },
                "BottomsGear": {
                    "skins": []
                }
            }
        },
        "inventory": [
            "HoneyBadger",
            "Vintorez",
            "Glock",
            "ff"
        ]
    }
}

The issue is when I want to give the user the weapons etc, I cannot get the first gun from the array when lua doesn’t know what gun it is since its a dictionary

But I need to make the weapon a dictionary to save attachments etc as seen in the Json layout.

How can I get the first name of the dictionary for example if it was an array I would do data.equipped.weapons[1]

Output would be

“Vintorez”

1 Like

To access a value in a dictionary you have to use the following format:
table[index]
Also you can repeat the index part as many times. It can also work like this if that index exists of course:
table[index][index2].index3
Use [] when for example the index is a string. Like table["hi"].

1 Like

Instead of storing weapons as a dictionary, you can store a list of dictionaries and put the name or ID of the weapon inside the dictionary:
"Weapons": [ { "id" : "Vintorez", "attachments": [], "skins": [] }, ... ]

1 Like

Thank you, I just realized dictionary is just like a folder, I did it like this:

Summary
"equipped": {
            "weapons": {
                "primaryOne": {
                    "name": "Vintorez",
                    "attachments": [],
                    "skins": []
                },
                "primaryTwo": {
                    "name": "HoneyBadger",
                    "attachments": [
                        "ACOG"
                    ],
                    "skins": []
                },
                "SideArm": {
                    "name": "ff",
                    "attachments": [],
                    "skins": []
                }
            }
        }

And just added “name” to it

1 Like

If you ever had the chance to learn JavaScript, you’ll notice that the layout of JSON files; is just a javascript object or as we know it in lua a dictionary.
That’s why JSON stands for JavaScript Object Notation.

3 Likes

I did not know that. Thank you for the information :slight_smile:

1 Like