0

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?

mpx
  • 3,081
  • 2
  • 26
  • 56
  • you've got a bad character in your json file. that additional back-tick. – ewokx Jul 17 '20 at 03:51
  • Does this answer your question? [How to convert JSON data into a Python object](https://stackoverflow.com/questions/6578986/how-to-convert-json-data-into-a-python-object) – mpx Jul 17 '20 at 09:27

1 Answers1

0

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)
NumberC
  • 596
  • 7
  • 17