Just_Newbie
Just_Newbie

Reputation: 447

How to read from a JSON with two keys

I have a json that I need to import and then return a certain value. The json has two keys, like

{
"NUM_High_Objects": {
 "abseta_pt": {
            "field1:[0.0,0.9]": {
                "field2:[15,20]": {
                   
                    "tagIso": 0.00012,
                    "value": 0.99
                },
                 "field2:[20,25]": {
                   
                    "tagIso": 0.00035,
                    "value": 0.98
                }
            },
            "field1:[0.91,1.2]": {
                "field2:[15,20]": {
                   
                    "tagIso": 0.00013,
                    "value": 0.991
                },
                 "field2:[20,25]": {
                   
                    "tagIso": 0.00036,
                    "value": 0.975
                     }
            },
            "binning": [
                {
                    "binning": [
                        0.0,
                        0.9,
                        1.2,
                        2.1,
                        2.4
                    ],
                    "variable": "abseta"
                },
                {
                    "binning": [
                        15,
                        20,
                        25,
                        30,
                        40,
                        50,
                        60,
                        120
                    ],
                    "variable": "pt"
                }
            ]
        }
    },

What I need is to search if a pair of values is within the range of "field1" and "field2" and return the corresponding "value"

I tried following this Search nested json / dict for multiple key values matching specified keys but could not make it to work...

I ve tried something like


class checkJSON() :

    def __init__(self,filein) :
        self.good, self.bad  = 0, 0
        print 'inside json function : will use the JSON', filein
        input_file = open (filein)
        self.json_array = json.load(input_file)

    def checkJSON(self,LS,run) :
        try :
            LSlist = self.json_array[str(run)]
            for LSrange in LSlist :print LSrange, run
        except KeyError :
            pass

        self.bad += 1
        return False

CJ=''
CJ=checkJSON(filein='test.json')
isInJSON = CJ.checkJSON("0.5", "20")

print isInJSON

but this does not work as I am not sure how to loop inside the keys

Upvotes: 0

Views: 125

Answers (1)

Shahan M
Shahan M

Reputation: 543

If I am understanding your question correctly then the relevant portion of your JSON is:

{
    "field1:[0.0,0.9]": {
        "field2:[15,20]": {
            "tagIso": 0.00012,
            "value": 0.99
        },
        "field2:[20,25]": {
            "tagIso": 0.00035,
            "value": 0.98
        }
    },
    "field1:[0.91,1.2]": {
        "field2:[15,20]": {
            "tagIso": 0.00013,
            "value": 0.991
        },
        "field2:[20,25]": {
            "tagIso": 0.00036,
            "value": 0.975
        }
    },
    "binning": [
        {
            "binning": [
                0.0,
                0.9,
                1.2,
                2.1,
                2.4
            ],
            "variable": "abseta"
        },
        {
            "binning": [
                15,
                20,
                25,
                30,
                40,
                50,
                60,
                120
            ],
            "variable": "pt"
        }
    ]
}

Then the following code should do what you are trying to achieve. It doesn't look like you need to search for nested keys, you simply need to parse your field1[...] and field2[...]. The code below is a quick implementation of what I understand you are trying to achieve. It will return the value if the first parameter is in the range of a field1[...] and the second parameter is in the range of a field2[...]. Otherwise, it will return None.

import json

def check_json(jsondict, l1val, l2val):
    def parse_key(keystr):
        level, lrange = keystr.split(':')
        return level, eval(lrange)

    for l1key, l2dict in jsondict.items():
        if 'field' in l1key:
            l1, l1range = parse_key(l1key)
            if l1val >= l1range[0] and l1val <= l1range[1]:
                for l2key, vals in l2dict.items():
                    l2, l2range = parse_key(l2key)
                    if l2val >= l2range[0] and l2val <= l2range[1]:
                        return vals['value']
    return None

Here is a driver code to test the implementation above.

if __name__ == '__main__':
    with open('data.json', 'r') as f:
        myjson = json.load(f)
    print(check_json(myjson, 0.5, 20))

Upvotes: 1

Related Questions