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))
2

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.