0

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))
Gaurang Shah
  • 11,764
  • 9
  • 74
  • 137
  • https://stackoverflow.com/questions/24898797/check-if-key-exists-and-iterate-the-json-array-using-python – David Zemens Mar 09 '18 at 16:51
  • 2
    If it works, and you wish to enhance/optimize your code, you should post on [code review](https://codereview.stackexchange.com/) – IMCoins Mar 09 '18 at 16:51

1 Answers1

0

Since you're looking for an easier/better approach, I recommend looking at Marshmallow for this kind of validation stuff. Here's a very rough example:

from marshmallow import Schema, fields

class EnviornmentSchema(Schema):
    username = fields.Str(required=True)
    scripts = fields.Str(required=True)
    params = fields.Str(required=True)

errors = EnviornmentSchema().validate(file_contents_dict)

Basically, it replaces your "template" system with a Schema class. You can use nesting too since you have multiple environment dicts. Marshmallow becomes very useful when you need to do more advanced validation.

Oles Tourko
  • 176
  • 1
  • 4
  • 10