Let’s assume your app needs variables width and height and you want your users to be able to change the values in a config file.
{ "width" : 1024, "height" : 768 }
To open 'config.JSON', you can use Pythons open function. To use the json load
function, you need to import the json
module. json.load
returns a dictionary that can be accessed by its keys. The syntax is data['width']
.
Here is the code:
import json with open('config.json') as config_file: data = json.load(config_file) width = data['width'] height = data['height'] print(width) print(height)
Open a terminal and execute the script with 'python loadconfig.py'. The result should look like this:
1024 768