I have the following JSON file
The JSON represents some roles, within the roles there are some categories such as MOD
and ADMINISTRATOR
and within these categories are the roles that will be assigned to a user, these roles contain the name of the role and a color.
If someone has a better way to structure the JSON can you tell me. Thank you
{
"roles":{
"MOD":[
["Mod","ORANGE"],
["Staff","GRAY"]
],
"ADMINISTRATOR":[
["ADMIN","RED"],
["CO-ADMIN","BLUE"],
["MANAGER","SKYBLUE"]
]
}
}
and what I want is to be able to access each of the objects that are inside roles
, but I don't know how to do this. i tried this.
const ROLES = require("roles.js")
const roles = ROLES.roles
for(rol of roles){
console.log(rol)
}
but it tells me UnhandledPromiseRejectionWarning: TypeError: roles is not iterable
so that I can't think of what I could do.
As I said, I want to obtain the arrays of MOD
y ADMINISTRATOR
but I want to do it dynamically, that is, to obtain all the arrays within roles since in the future I can add more.
As an extra piece of information when obtaining the arrays (MOD, ADMINISTRATOR, etc) I will have to access the elements that are inside these
As I see in your comments, you are looking dynamically to enter each role object and print the content of its array. I don't know if it's the most elegant but it's what comes to mind:
Your current JSON has two iterable elements (arrays) that are
MOD
andADMINISTRATOR
are inside the JSON objectroles
.You could iterate through them by accessing them directly, for example:
Edition
A structure that would better represent what you express in your question update would be the following.
The JSON you see in the code is briefly defined as a set of
roles
, all of them with a category (category
) and with a group of users (users
) which in turn have a propertyname
andcolor
. The most important of all this is that this JSON would represent a logic in your application expressed in a data model. Generally, this data model is coupled and works together with other tools, sometimes in different contexts such as databases, APIs, Web Services, mobile applications, web pages, etc. The data model is the same and then you express or extend it in all those parts where you need it.This JSON for example is mappable to a database table, to a class of type entity or POJO or any other type in a broader application.
The fundamental thing is that you respect the rules defined in the JSON , which would represent the logical way in which your application works in any context , so that it is flexible and can always be read with the same code.
Let's imagine that another role is added, the JSON will be modified, but NOT the code we used to read it:
Access specific elements within an array
If you want to access specific elements within an array you can use the index, keeping in mind that arrays are indexed from zero, so the first element would be at index
0
, the second element at index1
and so on.You can create references to elements:
Or access directly, browsing according to the json structure and indicating the indexes. For example:
Let's see a working example:
You must remember that when you go through an array you cannot get a value using the point nomenclature to resolve a value, you can only do it with objects, that is why in the for loop you have it gives you the error
TypeError: roles is not iterable
because it is not an array like such and you can't iterate it, what you should do is something like this:remember that in your json, the field
MOD
and the fieldADMINISTRATOR
are arrays, and that is what must be iterated, to be able to resolve an item from those arrays you could simply call the value using[0]
for the first item and so on.