I
I
IlyuhaShprotov2020-08-24 14:39:56
Python
IlyuhaShprotov, 2020-08-24 14:39:56

How can one access a key in a json dictionary through python?

I need to do a check if a key is in a dictionary in a json file.

import json

id = "2"

dict = json.load(open('C:/Users/ilyaf/Desktop/py-proj/Code Bot/assets/main.json'))

if id == dict.get(id):
  print('id in dict!')


when I run this code, nothing happens. Here is the json:
{
  "id":"2",
  "money":"100",
  "reputation":"0"
}


what's wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
kirillinyakin, 2020-08-24
@IlyuhaShprotov

It seems like your id in json is a string, and you are trying to compare it with an integer

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question