I want to read the json values from config json file. The json object path I use in my python code is like below:
jsonfile['Data'][0]['Tenants'][0]['TenPropertyGroup']
Now I want to pass this above path "['Data'][0]['Tenants'][0]['TenPropertyGroup']"
from ini file to make sure I can make changes in ini file if the object path gets changed in json file.
My config.ini
looks like:
[CONFIG]
TenPropertyGroup= ['Data'][0]['Tenants'][0]['TenPropertyGroups']
My python code after reading from ini file looks like
globalconfig = "config.ini"
config = configparser.ConfigParser()
config.read(globalconfig)
f = open(configfile, )
jsonfile = json.load(f)
TenPropertyGroup = config['CONFIG']['TenPropertyGroup']
TenPropertyGroups = (str(jsonfile ) + TenPropertyGroup)
But when I am reading in Python using configparser
the above PropertyGroup is of string data type and I am not able to get the list from json file.
I am trying to read this ini properly from python code but not able to convert it to object.
I would suggest a different approach. You should avoid executing text read from a file for security reasons. If you use a different format for you ini file value, you could parse it and use the values to drill down into you json object. Here's a simple example:
path_from_ini = 'x/y/z'
json_dict = { 'x' : { 'y' : { 'z' : 42 } } }
parts = path_from_ini.split('/')
v = json_dict
# Drill down into the JSON dictonary
for p in parts:
v = v[p]
print(v)