I am creating a JSON file in a particular format and so I want to validate if it's in that format or not before I process further.
This is how the template looks like
{
"dev":{
"username": "",
"script": "",
"params": ""
},
"qat":{
"username": "",
"script": "",
"params": ""
},
}
the file would be where all the values are filled it, for username
, script
and params
for both dev
and qat
Now I want to verify that, file with data is exactly same as the template. for now, I am using the following approach.
Approach Convert these files to dict and then get all the keys. And then compare these keys, if they are equal JSON file is as per template else not.
This works as expected, however just wanted to check if there is the better easier approach for this
Code:
def test_param_file():
with open('../utils/param_template.json') as json_data:
template = json.load(json_data)
with open('/file.json') as json_data:
param_file = json.load(json_data)
assert _get_all_keys(param_file) == _get_all_keys(template)
def _get_all_keys(param):
global prefix
global keys
keys = []
def func(param):
for key, value in param.iteritems():
if type(value) == dict:
global prefix
prefix = key
func(value)
global keys
keys.append("%s.%s" % (prefix, key))
func(param)
return list(set(keys))