0

I am trying to develop a discord bot with nodejs. My question is how do I get a specific data out of a JSON file (for instance, from this json. I want to only specify a single uuid like "59d4c622c7cc459a98c2e947054e2b11" and get the data.

1
  • 1
    Could you share your approach so far? Commented Feb 23, 2020 at 2:07

2 Answers 2

1

Assuming you have already parsed the JSON into an actual Javascript object named data and given the way the data is currently organized, you would have to search the data.guild.members array to find the object that had the desired uuid property value.

function findDataForUuid(uuid) {
     for (let obj of data.guild.members) {
        if (obj.uuid === uuid) {
            return obj;
        }
     }
     // no match found
     return null;
}


let item = findDataForUuid("59d4c622c7cc459a98c2e947054e2b11");
if (item) {
    console.log(item);    // will show all the properties of the member object
}

Or, using .find() on the array:

function findDataForUuid(uuid) {
     return data.guild.members.find(item => {
         return item.uuid === uuid;
     });
}
Sign up to request clarification or add additional context in comments.

3 Comments

can i get the rank and other data out of it then?
@Myzumi - Yes, this returns the whole object containing that uuid so you can read the .rank, .joined, .questParticipation, expHistory properties on the returned object.
@Myzumi - If this answered your question, then you can indicate that to the community here by clicking the checkmark to the left of the answer. That will also earn you some reputation points here for following the proper procedure.
0

The simple soln you can use filter. Best is use reduce.

const data = {
  guild: {
    _id: "5b2906070cf29ddccd0f203c",
    name: "Dulcet",
    coins: 122010,
    coinsEver: 232010,
    created: 1529415175560,
    members: [
      {
        uuid: "59d4c622c7cc459a98c2e947054e2b11",
        rank: "MEMBER",
        joined: 1529683128302,
        questParticipation: 39,
        expHistory: {
          "2020-02-16": 0,
          "2020-02-15": 0,
          "2020-02-14": 0,
          "2020-02-13": 0,
          "2020-02-12": 0,
          "2020-02-11": 0,
          "2020-02-10": 0
        }
      }
    ]
  }
};

const members = data.guild.members.filter(
  member => member.uuid == "59d4c622c7cc459a98c2e947054e2b11"
);

const firstMember = members[0]
console.log(firstMember)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.