0

I am coding a discord bot in javascript. I have a JSON file for warnings specific users have. the format is like this:

{
    "userOne": {
       "one": "spamming"
       "two": "sexism"
    }
    "userTwo": {
       "one": "spamming" 
    }
}

I need to be able to get information on a specific user. I know the name of the user, but am unable to get the information on them. I am currently using this code:

for (nick in warnings) {
            if (nick == user) {
                userWarnings=warnings.nick
                console.log(userWarnings);
            }
        }

warnings is the name of the file that has been parsed. user is the name of the user I want to find the warnings for (Eg: userTwo).

when I run the code, the value of userWarnings is undefined. I know why this is, I just don't know how to fix it.

conclusion: is there a way to find the information on a user if I know their name.

  • There doesn't seem to be any arrays in your JSON file, see https://stackoverflow.com/questions/11922383/how-can-i-access-and-process-nested-objects-arrays-or-json – Teemu Nov 06 '20 at 21:33
  • right - that JSON is not really valid JSON nor is it a JavaScript object. – Randy Casburn Nov 06 '20 at 21:34
  • Assuming the _actual_ JavaScript object _is actually correct_, `.nick` is not a property of `warnings`. But `warnings[nick]` probably is. – Randy Casburn Nov 06 '20 at 21:36
  • I think that it is a javascript object, if byt hat you mean running this: ```let warnings = fs.readFileSync('./warnings.json'); warnings=(JSON.parse(warnings))``` –  Nov 06 '20 at 22:14

1 Answers1

0

Thanks to Randy Casburn, I now know the answer. warnings.nick was not valid, but warnings[nick] is.