0

I want to store JSON attributes in a dict or list whatever will be the best option to store it. The JSON looks like this:

{
"@BOOLOP": "and",
"@SEQ": "0",
"@TYPE": "0",
"FRAGMENT": {
    "@FUNC1": "value",
    "@FUNC2": "literal",
    "@OP": "<",
    "@PROP1": "PB:FeederSpeed",
    "@PROP2": "0",

},
"BOOLOP": {
    "@BOOLOP": "or",
    "FRAGMENT": [
        {
            "@FUNC1": "value",
            "@FUNC2": "literal",               
            "@OP": "=",
            "@PROP1": "PB:CPProdSelected",
            "@PROP2": "1000",
        }

    ]
}
}

Basically the format of this JSON is that is will have a BOOLOP attibs then a FRAGMENT key (depends) followed by n no. of BOOLOP as Keys.

The point where I am strugling is how to store it in a suitable data structure so that it is easy to perform operations on.

For example , I need to perform operations on FRAGMENT key but for that I need to check if it has all the conditions meeting or not. Like the @BOOLOP is and or or and as there are multiple BOOLOP key inside key it is difficult to perform operations upon.

I came this far by extracting the part of JSON I need from a bog JSON but now I am stuck as how to do a recursive call and check if a FRAGMENT is inside a BOOLOP.

The JSON I have posted is only a sample on the original file could have n number of @BOOLOP but I can figure that part out after I have my first sample in place.

EDIT

I thought about storing the JSON inside a tree Tree Scenario

Duck_dragon
  • 440
  • 5
  • 17

1 Answers1

0

As you say, there are multiple BOOLOP key inside the json, it would better to append all values of BOOLOP keys of the json into a list, which would result into a list of dictionaries.

Now, you can loop through that list of dictionaries and check for each element whether the FRAGMENT key is inside that item.

Consider, a sample list of dictionaries made from multiple BOOLOP keys of the json :

a = [{
"@BOOLOP": "or",
"FRAGMENT": [
    {
        "@FUNC1": "value",
        "@FUNC2": "literal",               
        "@OP": "=",
        "@PROP1": "PB:CPProdSelected",
        "@PROP2": "1000",
    }
    ]
},
{
"@BOOLOP": "and",
"FRAGMENT": [
    {
        "@FUNC1": "va1lue",
        "@FUNC2": "lite56ral",               
        "@OP": "=0",
        "@PROP1": "P85B:CPProdSelected",
        "@PROP2": "10000",
    }
    ]
}

Now check inside this list, with this :

for i in a:
    # Check if "@BOOLOP" key is in i, if present then proceed
    if "@BOOLOP" in i:
        if "FRAGMENT" in i:
            # Perform operations on "FRAGMENT" key
Arkistarvh Kltzuonstev
  • 6,824
  • 7
  • 26
  • 56