Home > Software design >  Python cannot find key in a json
Python cannot find key in a json

Time:12-10

I have the following json in a text file:

{
"defaultSymbol" : {
          "type" : "CIMSymbolReference",
          "symbol" : {
            "type" : "CIMPointSymbol",
            "symbolLayers" : [
              {
                "type" : "CIMVectorMarker",
                "enable" : true,
                "anchorPointUnits" : "Relative",
                "dominantSizeAxis3D" : "Z",
                "size" : 4,
                "billboardMode3D" : "FaceNearPlane",
                "frame" : {
                  "xmin" : -2,
                  "ymin" : -2,
                  "xmax" : 2,
                  "ymax" : 2
                },
                "markerGraphics" : [
                  {
                    "type" : "CIMMarkerGraphic",
                    "geometry" : {
                      "curveRings" : [
                        [
                          [
                            1.2246467991473532e-16,
                            2
                          ],
                          {
                            "a" : [
                              [
                                1.2246467991473532e-16,
                                2
                              ],
                              [
                                0,
                                0
                              ],
                              0,
                              1
                            ]
                          }
                        ]
                      ]
                    },
                    "symbol" : {
                      "type" : "CIMPolygonSymbol",
                      "symbolLayers" : [
                        {
                          "type" : "CIMSolidStroke",
                          "enable" : true,
                          "capStyle" : "Round",
                          "joinStyle" : "Round",
                          "lineStyle3D" : "Strip",
                          "miterLimit" : 10,
                          "width" : 1,
                          "color" : {
                            "type" : "CIMRGBColor",
                            "values" : [
                              0,
                              0,
                              0,
                              100
                            ]
                          }
                        },
                        {
                          "type" : "CIMSolidFill",
                          "enable" : true,
                          "color" : {
                            "type" : "CIMRGBColor",
                            "values" : [
                              0,
                              3,
                              173,
                              100
                            ]
                          }
                        }
                      ]
                    }
                  }
                ],
                "respectFrame" : true
              }
            ],
            "haloSize" : 1,
            "scaleX" : 1,
            "angleAlignment" : "Map"
          }
        }
}

I also have the following python code to print each key value pair recursively:

def recursive_items(dictionary):
    for key, value in dictionary.items():
        if type(value) is dict:
            print("value is dict: "  str(type(value)))
            yield (key, value)
            yield from recursive_items(value)
        else:
            print("value is not dict: "  str(type(value)))
            yield (key, value)

f = open('TestJsonMultipleSymbols.txt')

# returns JSON object as
# a dictionary
data = json.load(f)

for key, value in recursive_items(data):
    print("Key: "   str(key), "Value: "   str(value))
    print("Next")
print("done")

This prints all the keys and shows 'symbol' as a key which I looking to find and print the value of. However when I search for the key 'symbol' by modifying the code it returns nothing. What am I missing here? Here is the modified code:

def recursive_items(dictionary):
    for key, value in dictionary.items():
        search_key = 'symbol'
        if search_key in dictionary: 
            if type(value) is dict:
                print("value is dict: "  str(type(value)))
                yield (key, value)
                yield from recursive_items(value)
            else:
                print("value is not dict: "  str(type(value)))
                yield (key, value)

CodePudding user response:

Well simply the check if search_key in dictionary is not recursive. On your very first call, it will check whether symbol is a key in the outermost dictionary. The top-level.

Now, symbol isn't a key in that dict, so the function just exits without doing anything.

  • Related