How can I get all the name parts of all the second choices in a json object

This is the full object that I want to get something from is:

{
    "name": "permissions",
    "description": "Get or edit permissions for a user or a role",
    "options": [
        {
            "name": "user",
            "description": "Get or edit permissions for a user",
            "type": 2, 
            "options": [
                {
                    "name": "get",
                    "description": "Get permissions for a user",
                    "type": 1, 
                    "options": [
                        {
                            "name": "user",
                            "description": "The user to get",
                            "type": 6,
                            "required": true
                        },
                        {
                            "name": "channel",
                            "description": "The channel permissions to get. If omitted, the guild permissions will be returned",
                            "type": 7,
                            "required": false
                        }
                    ]
                },
                {
                    "name": "edit",
                    "description": "Edit permissions for a user",
                    "type": 1,
                    "options": [
                        {
                            "name": "user",
                            "description": "The user to edit",
                            "type": 6,
                            "required": true
                        },
                        {
                            "name": "channel",
                            "description": "The channel permissions to edit. If omitted, the guild permissions will be edited",
                            "type": 7,
                            "required": false
                        }
                    ]
                }
            ]
        },
        {
            "name": "role",
            "description": "Get or edit permissions for a role",
            "type": 2,
            "options": [
                {
                    "name": "get",
                    "description": "Get permissions for a role",
                    "type": 1,
                    "options": [
                        {
                            "name": "role",
                            "description": "The role to get",
                            "type": 8, 
                            "required": true
                        },
                        {
                            "name": "channel",
                            "description": "The channel permissions to get. If omitted, the guild permissions will be returned",
                            "type": 7,
                            "required": false
                        }
                    ]
                },
                {
                    "name": "edit",
                    "description": "Edit permissions for a role",
                    "type": 1,
                    "options": [
                        {
                            "name": "role",
                            "description": "The role to edit",
                            "type": 8,
                            "required": true
                        },
                        {
                            "name": "channel",
                            "description": "The channel permissions to edit. If omitted, the guild permissions will be edited",
                            "type": 7,
                            "required": false
                        }
                    ]
                }
            ]
        }
    ]
}

I want to get all of these name attributes from the first json object and assign them all to different variables in javascript? How can I achieve this?

Permissions —> role —> edit —> channel (In other words I want to get all of the name parts in all the second choices in the json object)

39 thoughts on “How can I get all the name parts of all the second choices in a json object”

  1. If you know that JSON structure is fixed then it will be easy.

    Consider your json is assigned to permissionJson then you can write it as

    permissionJson.name  // permissions
    permissionJson.options[1].name. // role
    permissionJson.options[1].options[1].name // edit
    permissionJson.options[1].options[1].options[1].name // channel
    
    

    if JSON structure is dynamic then you might need loops to extract the options array index 1 element at each level and find its name.

    Reply
  2. first of all i want to make clear that i don’t understand your question very well. So correct me if i misunderstood something.
    you said you want to get all name parts in all second member of options(root one) array. so all you have to do is

    JSON_DATA.options[1]
    

    Here you have selected whole role object. to get more deeper you can use dot operator. For instance Permissions —> role —> edit —> channel. this could be accessed by this line JSON_DATA.options[1].options[1]
    it will return this

    {
      "name": "channel",
      "description": "The channel permissions to get. If omitted, the guild permissions will be returned",
      "type": 7,
      "required": false
     }
    
    Reply

Leave a Comment