0

This may look like a basic questions, but here is what I am trying to do:

This is my current JSON structure:

{
  "11": {
    "14": [
      "704712637988077XXX"
    ],
    "16": [
      "704712637988077XXX"
    ],
    "17": [
      "704712637988077XXX"
    ]
  }
}

Whereas 11 stands for the month, 14/16 and 17 for a date and the number inside the list is a user ID. What I am trying to achieve is that my code checks if the ID of the user exists somewhere, taking into account all the dates or months saved in the JSON. (There could be more months and dates!)

Is there any function that takes care of this?

I checked the following posts on this:

and much more but I am kind of stuck.

What I tried to do:

str(ctx.author.id) in any(data[str(month)][str(day)]) # Should return True or False but is somehow inconsistent
str(ctx.author.id) and any(data[str(month)][str(day)]) == True # Does not make sense at all, I guess
str(ctx.author.id) in any(data[str(month)][str(day)]).__str__() # Convert everything and check for the value

data is of course the JSON I open, month and day are converted inputs of the user that are later displayed as 11 for the month and 14, 16, 17... as days in the JSON.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Dominik
  • 3,612
  • 2
  • 11
  • 44
  • if you are going to try and check multiple IDs than it might be recommended that you read through your data and create a set of IDs that you can quickly reference - knowing that those are the only IDs in the data. – Andrew Ryan May 26 '22 at 02:49
  • Why are the `month` and `day` inputs converted? Why not leave them as strings if you're just going to cast them back to `str`? – ddejohn May 26 '22 at 02:51
  • 1
    Can you clarify your expected output? Do you want a list of all dates where the user id is present? Or are you looking for a specific date? Your use of `any()` is confusing. – ddejohn May 26 '22 at 02:52
  • @ddejohn I want to prevent that a user is present more than one time. A user should only be able to submit one month/date and if I check for the user again and it is present somewhere in the JSON, I will drop the old value and insert the new value. – Dominik May 26 '22 at 02:58
  • What? Your question is now even more unclear. Your question is originally about searching for a user id in a nested dictionary/json. Now you're including additional constraints and also trying to modify.... what, exactly? – ddejohn May 26 '22 at 02:59
  • @ddejohn "*What I am trying to achieve is that my code checks if the ID of the user exists somewhere*" - I only want to get a `True` or `False` there. What I am doing afterwards was just an explanation, not relevant for this question. – Dominik May 26 '22 at 03:01
  • You say that `month` and `day` are inputs from the user... so they're specific values. Which implies that you're simply asking if the user id is present on that month/day in your data. Your comments seem to imply that that is *not* what you're trying to do? – ddejohn May 26 '22 at 03:01
  • 2
    @MikeTeston Did you see the duplicate I linked? If it doesn't answer your question, can you please explain how? This would help clarify the question, given that one of the answers is literally just an answer from that post. – TheFungusAmongUs May 26 '22 at 03:01
  • I think you're just looking for `str(ctx.author.id) in data[str(month)][str(day)]` – Nick May 26 '22 at 03:02
  • @TheFungusAmongUs This looks like I would have to take care of every `month` that is inserted myself, at least from some answers. I am currently trying to take all comments and answers into account and test them. – Dominik May 26 '22 at 03:07
  • @Nick I also tried to use this method, and it should indeed show me the ID of the author or check it but trying to print/check this it just does not even fire at all. – Dominik May 26 '22 at 03:11
  • 3
    Since you only want to know if an ID exist all you need to do is iterate over the deepest keys. `any([ctx.author.id] in inner.values() for inner in data.values())` you don't need to pass any `month` or `day` – 2293980990 May 26 '22 at 03:12
  • @MikeTeston You could simply use the generator in the linked post, and then do `if str(ctx.author.id) in NestedDictValues(your_dictionary)` – TheFungusAmongUs May 26 '22 at 03:16
  • 1
    Welcome to Stack Overflow. Yes, you do need to have some approach to the code that searches the data structure, depending on what the rules are for where the "ID" might be found. The linked duplicate should be enough to understand the general technique. Having loaded the JSON, you have *just* a nested structure of dictionaries, which you handle *the same way* that you would if you had gotten *that same data, in any other way*. – Karl Knechtel May 26 '22 at 03:19
  • Also: please don't use tags that are irrelevant to the question. The fact that you want to use this for a Discord bot doesn't make `discord.py` relevant; people who are experts in the Discord API will have no special advantage in understanding or answering the question, since it is purely about data structures. – Karl Knechtel May 26 '22 at 03:20

1 Answers1

-2

Why did you use "any"? Please try

str(ctx.author.id) in data[str(month)][str(day)]
birdtiger
  • 71
  • 2
  • Unfortunately, this does not work. In earlier tests this printed me the ID which is what I am kind of looking for but having some checks beforehand (or even without them) it is not doing what I try to achieve. – Dominik May 26 '22 at 03:12