I have a JSON file (config.json
) which looks like this:
{
"name":"Ram"
}
And a script looks like this:
f=open('config.json',)
name=json.load(f)
print(name)
Then, how to assign the local variable (i.e., name)
into Python code?
I have a JSON file (config.json
) which looks like this:
{
"name":"Ram"
}
And a script looks like this:
f=open('config.json',)
name=json.load(f)
print(name)
Then, how to assign the local variable (i.e., name)
into Python code?
You would treat it like any other dictionary. myDictionary[myKey]
would equal myValue
for a JSON like {myKey: myValue}
. Your code should look like this:
f=open('config.json',)
jsonF=json.load(f)
name = jsonF["name"] #THIS is how to get the value from a key in a dictionary
print(name)