简体   繁体   中英

Parsing nested objects from Json file with Node.js

I have a Json file which is effectively a 2D array. The array contains arrays of Discord Server IDs. these arrays of Server IDs, contain User IDs. The user ID objects contain data relevant for the discord bot.

{
    "Servers":[
        {
            "724992148444938330":[ //This is a Server ID
                {
                    "110596839018856448":{ //This is a UserID
                        "userID":"110596839018856448",
                        "CurrentChar":"samplechar.json",
                        "CurrentCharName":"Bob",
                        "CurrentGame":"StructureIdea.json"
                    }
                },
                {
                    "110596839018856449":{
                        "userID":"110596839018856449",
                        "CurrentChar":"samplechar.json",
                        "CurrentCharName":"Bob",
                        "CurrentGame":"StructureIdea.json"
                    }
                },
                {
                "110596839018856450":{
                        "userID":"110596839018856450",
                        "CurrentChar":"samplechar.json",
                        "CurrentCharName":"Bob",
                        "CurrentGame":"StructureIdea.json"
                    }
                }
            ]
        },
        {    
            "724992148444938331":[
                {
                    "110596839018856448":{
                        "userID":"110596839018856448",
                        "CurrentChar":"samplechar.json",
                        "CurrentCharName":"Bob",
                        "CurrentGame":"StructureIdea.json"
                    }
                },
                {
                    "110596839018856449":{
                        "userID":"110596839018856449",
                        "CurrentChar":"samplechar.json",
                        "CurrentCharName":"Bob",
                        "CurrentGame":"StructureIdea.json"
                    }
                },
                {
                "110596839018856450":{
                        "userID":"110596839018856450",
                        "CurrentChar":"samplechar.json",
                        "CurrentCharName":"Bob",
                        "CurrentGame":"StructureIdea.json"
                    }
                }
            ]
        },
        {
            "724992148444938332":[
                {
                    "110596839018856448":{
                        "userID":"110596839018856448",
                        "CurrentChar":"samplechar.json",
                        "CurrentCharName":"Bob",
                        "CurrentGame":"StructureIdea.json"
                    }
                },
                {
                    "110596839018856449":{
                        "userID":"110596839018856449",
                        "CurrentChar":"samplechar.json",
                        "CurrentCharName":"Bob",
                        "CurrentGame":"StructureIdea.json"
                    }
                },
                {
                "110596839018856450":{
                        "userID":"110596839018856450",
                        "CurrentChar":"samplechar.json",
                        "CurrentCharName":"Bob",
                        "CurrentGame":"StructureIdea.json"
                    }
                }
            ]
        }
    ]
}

What i want to do and why it doesn't work:

Im trying to retrieve data from the User objects. The problem is, I only ever get back 'undefined' or 'object Object' when i try to even adress the ServerID array, let alone the array of users. Here is the code i have so far. (Im very new to Javascrips so excuse me if i ask some stupid questions)

    function getCurrentCharName(serverID,userID){  //retrieve current char name from user ID and return it
    serverID = "724992148444938330" //Server ID im trying to address
    userID = "110596839018856448"  //User im trying to address
    var userPath = "./Resources/UserData/user.json" //path of json File
    var Data = fs.readFileSync(userPath);
    Data = JSON.parse(Data);

    console.log(Data.Servers.find(itm => itm == 724992148444938330)) //returns 'undefined'
    console.log(Data.Servers.724992148444938330); //doesnt allow that
    console.log(Data.Servers.serverID); //returns undefined


    for(var itm in Data.Servers){ //returns '[object Object]' 
        console.log(itm + ": " + Data.Servers[itm]);
    }
    };

What i would like it to do/return:

I want to grab specific sets of data, depending on what serverID and userID is. That means if i want to grab CurrentChar, id like to do something along the lines of

aChar = Data.Servers.serverID.userID.CurrentChar

This should return the string 'samplechar.json.

Like i said, im new to Javascript and Json so there might be a pretty obvious flaw here but id be thankful for any help with this.

Data.Servers.find(itm => itm == 724992148444938330)

What you're telling js to do here is basically go one by one through the array and see if that array index is equal to '724992148444938330'. However, itm looks like this

        {
            "724992148444938330":[ //This is a Server ID
                {
                    "110596839018856448":{ //This is a UserID
                        "userID":"110596839018856448",
                        "CurrentChar":"samplechar.json",
                        "CurrentCharName":"Bob",
                        "CurrentGame":"StructureIdea.json"
                    }
                },
                {
                    "110596839018856449":{
                        "userID":"110596839018856449",
                        "CurrentChar":"samplechar.json",
                        "CurrentCharName":"Bob",
                        "CurrentGame":"StructureIdea.json"
                    }
                },
                {
                "110596839018856450":{
                        "userID":"110596839018856450",
                        "CurrentChar":"samplechar.json",
                        "CurrentCharName":"Bob",
                        "CurrentGame":"StructureIdea.json"
                    }
                }
            ]
        }

So itm == 724992148444938330 will never pass. instead what should be done is something like:

   let servers = Object.entries(Data.Servers);

servers should now be an array like:

    [ 
        ['724992148444938330', [the value of '724992148444938330']
    ]

The key of the object is stored in index 0 and the value is stored in index 1. So now in order to find server '724992148444938330' you can do:

let serverNeeded = servers.find(itm => itm[0] == 724992148444938330)

Or you can be more specific and do something like:

let serverNeeded = servers[0][0][1]

In either case, serverNeeded should now equal this array:

            [ //This is a Server ID
                {
                    "110596839018856448":{ //This is a UserID
                        "userID":"110596839018856448",
                        "CurrentChar":"samplechar.json",
                        "CurrentCharName":"Bob",
                        "CurrentGame":"StructureIdea.json"
                    }
                },
                {
                    "110596839018856449":{
                        "userID":"110596839018856449",
                        "CurrentChar":"samplechar.json",
                        "CurrentCharName":"Bob",
                        "CurrentGame":"StructureIdea.json"
                    }
                },
                {
                "110596839018856450":{
                        "userID":"110596839018856450",
                        "CurrentChar":"samplechar.json",
                        "CurrentCharName":"Bob",
                        "CurrentGame":"StructureIdea.json"
                    }
                }
            ]

And to parse this we would need to follow the same method we did earlier:

let firstUser = Object.entries(serverNeeded[0])
console.log(firstUser[0], firstUser[1])
let userID = firstUser[1].userID
console.log(userID)

I'm sure you can come up with a way to automate this. Also, if you would like to learn more about Object.entries() this is a great resource: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries

There are a bunch of other useful ways to go about this that are on this website as well.

I believe what you're looking for is:

console.log( Data.Servers.find(itm => Object.keys(itm).includes("724992148444938330")) );

whose output would be something like:

{ 
    '724992148444938330': [{ 
        '110596839018856448': <Object> 
    }, { 
        '110596839018856449': <Object> 
    }, { 
        '110596839018856450': <Object> 
    }] 
}

In the same way, you can also drill down further like so:

console.log( Data.Servers.find(itm => Object.keys(itm).includes("724992148444938330"))["724992148444938330"].find(usr => Object.keys(usr).includes('110596839018856448')) );

Output:

{ 
    '110596839018856448': { 
        userID: '110596839018856448',
        CurrentChar: 'samplechar.json',
        CurrentCharName: 'Bob',
        CurrentGame: 'StructureIdea.json' 
    } 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM