json

In [ ]:
# config.json
{
    "market1":
    {
        "name":"market1",
        "dimension1":
        {
            "table":"dim1",
            "fields":"product_id,product_desc,product_l1,product_l2, prodl3",
            "hierarchy_flag":true,
            "linear_hierarchy_flag":true,
            "hierarchy":"product_id,product_l1,product_l2",
            "dependent_field_flag":true,
            "keys":
            [
                {"pk":"product_id","sk":"product_desc","order":0},
                {"pk":"product_l1","sk":"product_l1_desc","order":1},
                {"pk":"product_l2","sk":"product_l2_desc","order":2}
            ]
        },
        "dimension2":
        {
            "table":"dim2",
            "fields":"store_id,store_name,size,region",
            "hierarchy_flag":true,
            "linear_hierarchy_flag":false,
            "hierarchy":9,
            "dependent_field_flag":true,
            "keys":
            [
                {"pk":"store_id","sk":"store_id,store_name","order":0},
                {"pk":"size","sk":"size_id,size_desc","order":1}
            ]
        },
        "dimension3":
        {
            "table":"dim3",
            "fields":"customer_id",
            "hierarchy_flag":false,
            "dependent_field_flag":false,
            "key1":"customer_id"
        }
    }
}
In [1]:
import json
with open('config.json') as config_json:
    dict_config = json.load(config_json)
    
print(dict_config)
{'market1': {'name': 'market1', 'dimension3': {'table': 'dim3', 'key1': 'customer_id', 'dependent_field_flag': False, 'hierarchy_flag': False, 'fields': 'customer_id'}, 'dimension2': {'keys': [{'pk': 'store_id', 'order': 0, 'sk': 'store_id,store_name'}, {'pk': 'size', 'order': 1, 'sk': 'size_id,size_desc'}], 'linear_hierarchy_flag': False, 'hierarchy_flag': True, 'hierarchy': 9, 'fields': 'store_id,store_name,size,region', 'table': 'dim2', 'dependent_field_flag': True}, 'dimension1': {'keys': [{'pk': 'product_id', 'order': 0, 'sk': 'product_desc'}, {'pk': 'product_l1', 'order': 1, 'sk': 'product_l1_desc'}, {'pk': 'product_l2', 'order': 2, 'sk': 'product_l2_desc'}], 'linear_hierarchy_flag': True, 'hierarchy_flag': True, 'hierarchy': 'product_id,product_l1,product_l2', 'fields': 'product_id,product_desc,product_l1,product_l2, prodl3', 'table': 'dim1', 'dependent_field_flag': True}}}
In [2]:
def get_config_key(dict_config, l_search_key):
    for key1, value1 in dict_config.items():  # market1
        if dict_config[key1]['name'].lower() == 'market1'.lower():  # market name
            # dimension1
            for dict1 in dict_config[key1]['dimension1']['keys']:
                for key2, value2 in dict1.items():
                    if key2 == 'sk':
                        if l_search_key.lower() in value2.split(','):
                            l_pk = dict1['pk']
                            l_sk = l_search_key
                            l_order = dict1['order']
                            l_table = dict_config[key1]['dimension1']['table']
                            l_subject = 'dimension1'
                            return ([l_subject, l_table, l_pk, l_sk, l_order])
            # dimension2
            for dict1 in dict_config[key1]['dimension2']['keys']:
                for key2, value2 in dict1.items():
                    if key2 == 'sk':
                        if l_search_key.lower() in value2.split(','):
                            l_pk = dict1['pk']
                            l_sk = l_search_key.lower()
                            l_order = dict1['order']
                            l_table = dict_config[key1]['dimension2']['table']
                            l_subject = 'dimension2'
                            return ([l_subject, l_table, l_pk, l_sk, l_order])
            # dimension3
            for key2, value2 in value1.items():
                if key2.lower() in ['dimension3', 'dimension4']:
                    for key3, value3 in value2.items():
                        if key3[0:3].lower() == 'key':
                            if value3.lower() == l_search_key.lower():
                                print(value3)
                                l_value = dict_config[key1][key2][key3]
                                l_table = dict_config[key1][key2]['table']
                                l_subject = key2
                                return ([l_subject, l_table, l_value])

get_config_key(dict_config,'product_desc')
Out[2]:
['dimension1', 'dim1', 'product_id', 'product_desc', 0]
www.000webhost.com