pythonjsonpython-3.x

Can't access json property


I'm trying to access the 'city' property of this json but somehow it doesn't work.

This is the json struct:

"{\"ForSaleShopperPlatformFullRenderQuery{\\\"zpid\\\":28657235,\\\"platform\\\":\\\"DESKTOP_WEB\\\",\\\"formType\\\":\\\"OPAQUE\\\",\\\"contactFormRenderParameter\\\":{\\\"zpid\\\":28657235,\\\"platform\\\":\\\"desktop\\\",\\\"isDoubleScroll\\\":true},\\\"skipCFRD\\\":false,\\\"ompPlatform\\\":\\\"web\\\"}\":{\"property\":{\"listingDataSource\":\"Phoenix\",\"zpid\":28657235,\"city\":\"Boerne\",\"state\":\"TX\",\"homeStatus\":\"FOR_SALE\",\"address\":{\"streetAddress\":\"111 stone creek\",\"city\":\"Boerne\",\"state\":\"TX\",\"zipcode\":\"78006\",\"neighborhood\":null,\"community\":null,\"subdivision\":null},\"isListingClaimedByCurrentSignedInUser\":false,\"isCurrentSignedInAgentResponsible\":false,\"bedrooms\":3,\"bathrooms\":2,\"price\":345000,\"yearBuilt\":1999,\"streetAddress\":\"111 stone creek\",\"zipcode\":\"78006\",\"isCurrentSignedInUserVerifiedOwner\":false,\"propertyUpdatePageLink\":null,\"moveHomeMapLocationLink\":null,\"propertyEventLogLink\":null,\"editPropertyHistorylink\":null,\"collections\":{\"modules\":[{\"name\":\"Similar homes\",\"placement\":\"NEIGHBORHOOD\",\"propertyDetails\":[{\"miniCardPhotos\":[{\"url\":\"https://photos.zillowstatic.com/fp/219307696092bb2d5e31698cba1c1e1f-p_c.jpg\"}],\"price\":315000,\"currency\":\"USD\",\"bedrooms\":3,\"bathrooms\":3,\"livingArea\":1729,\"livingAreaValue\":1729,\"livingAreaUnits\":\"Square Feet\",\"livingAreaUnitsShort\":\"sqft\",\"listingMetadata\":{\"cominglingCategoryIsRulesApplicable\":true},\"lotSize\":2874,\"lotAreaValue\":2874.96,\"lotAreaUnits\":\"Square Feet\",\"address\":{\"streetAddress\":\"130 Hampton Bend\",\"city\":\"Boerne\",

{
    "ForSaleShopperPlatformFullRenderQuery{\"zpid\":28657235,\"platform\":\"DESKTOP_WEB\",\"formType\":\"OPAQUE\",\"contactFormRenderParameter\":{\"zpid\":28657235,\"platform\":\"desktop\",\"isDoubleScroll\":true},\"skipCFRD\":false,\"ompPlatform\":\"web\"}": {
        "property": {
            "listingDataSource": "Phoenix",
            "zpid": 28657235,
            "city": "Boerne",
            "state": "TX",
            "homeStatus": "FOR_SALE",
            "address": {
                "streetAddress": "111 stone creek",
                "city": "Boerne",
                "state": "TX",
                "zipcode": "78006",
                "neighborhood": null,
                "community": null,
                "subdivision": null
            },

this is the code :

key = 'ForSaleShopperPlatformFullRenderQuery{"zpid":28657235,"platform":"DESKTOP_WEB","formType":"OPAQUE","contactFormRenderParameter":{"zpid":28657235,"platform":"desktop","isDoubleScroll":true},"skipCFRD":false,"ompPlatform":"web"}'
        city = data["props"]["pageProps"]["componentProps"]["gdpClientCache"][key]['property']['city']

i get error :

print(data["props"]["pageProps"]["componentProps"]["gdpClientCache"]['ForSaleShopperPlatformFullRenderQuery{\\"zpid\":28657235,\\"platform\\":\\"DESKTOP_WEB\\",\\"formType\\":\\"OPAQUE\\",\\"contactFormRenderParameter\\":{\\"zpid\\":28657235,\\"platform\\":\\"desktop\\",\\"isDoubleScroll\\":true},\\"skipCFRD\\":false,\\"ompPlatform\\":\\"web\\"}'])
          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: string indices must be integers, not 'str'

tried also

key = "ForSaleShopperPlatformFullRenderQuery{\"zpid\":28657235,\"platform\":\"DESKTOP_WEB\",\"formType\":\"OPAQUE\",\"contactFormRenderParameter\":{\"zpid\":28657235,\"platform\":\"desktop\",\"isDoubleScroll\":true},\"skipCFRD\":false,\"ompPlatform\":\"web\"}"

still doesn't work


Solution

  • First of all, your json example is not complete as I can't see these keys.

    ["props"]["pageProps"]["componentProps"]["gdpClientCache"]

    So I tried data[key]['property']['city'], and it works. result

    I think the problem is at this key gdpClientCache. It must have returned a string value but not the json example you showed us. That's why the program wants you to use an integer as the index.

    Double check the return value of each key and you should be able to solve the problem.