Search code examples
pythonjsonstringdictionary

accessing a string in a nested dictionary


I have a nested json file, i am trying to access the latitude , i tried the code below latitude = data["props"]["pageProps"]["property"]["address"]["latitude"] but i got an error. I also tried loading it in a data_loaded = json.loads(data) but got the error in the picture attached below "Expecting ',' delimiter: line 1 column 53 (char 52)"

data = '{
  "props": {
    "pageProps": {
      "property": {
        "uprn": "61019296",
        "address": {
          "uprn": "61019296",
          "fullAddress": "8 Arthur Street Barnoldswick BB18 5JZ",
          "postcode": "BB18 5JZ",
          "country": "England",
          "outcode": "BB18",
          "latitude": 53.9229587,
          "longitude": -2.1940256
        },
        "attributes": {
          "bathrooms": null,
          "bedrooms": 3,
          "floorAreaSqM": 109,
          "livingRooms": null,
          "tenure": "Freehold",
          "propertyType": "Mid Terrace House",
          "userSubmitted": null
        },
        "energy": {
          "currentEnergyRating": "C"
        },
        "rentEstimate": {
          "lowerPrice": 700,
          "currentPrice": 750,
          "upperPrice": 800
        },
        "saleEstimate": {
          "lowerPrice": 128000,
          "currentPrice": 135000,
          "upperPrice": 141000,
          "confidenceLevel": "HIGH",
          "valueChange": {
            "numericChange": -16000,
            "percentageChange": -10.596026490066226,
            "saleDate": "2024-12-20"
          },
          "ingestedAt": "2025-02-06T14:05:28.374Z"
        },
        "history": {
          "historicListings": [],
          "historicSales": [
            {
              "date": "2024-12-20",
              "price": 151000,
              "percentageChange": 41.1214953271028,
              "numericChange": 44000
            },
            {
              "date": "2024-01-10",
              "price": 107000,
              "percentageChange": 3.3816425120772946,
              "numericChange": 3500
            },
            {
              "date": "2007-01-19",
              "price": 103500,
              "percentageChange": 168.8311688311688,
              "numericChange": 65000
            },
            {
              "date": "2001-05-25",
              "price": 38500,
              "percentageChange": null,
              "numericChange": null
            }
          ],
          "soldPricesDataSource": "landRegistry"
        },
        "staticMapImage": {
          "devices": {
            "mobile": 0,
            "mobile2x": 1,
            "medium": 2,
            "large": 3
          },
          "urls": [
            {
              "height": 200,
              "width": 400,
              "src": "https://maps.zoopla.co.uk/styles/portal/static/-2.194025653.922958716/400x200.webp?marker=-2.1940256%2C53.9229587%7Chome-property-pin%402x.png%7Cscale%3A0.5"
            },
            {
              "height": 200,
              "width": 400,
              "src": "https://maps.zoopla.co.uk/styles/portal/static/-2.194025653.922958716/400x200@2x.webp?marker=-2.1940256%2C53.9229587%7Chome-property-pin%402x.png%7Cscale%3A0.5"
            },
            {
              "height": 170,
              "width": 1024,
              "src": "https://maps.zoopla.co.uk/styles/portal/static/-2.194025653.922958716/1024x170.webp?marker=-2.1940256%2C53.9229587%7Chome-property-pin%402x.png%7Cscale%3A0.5"
            },
            {
              "height": 170,
              "width": 1024,
              "src": "https://maps.zoopla.co.uk/styles/portal/static/-2.194025653.922958716/1024x170@2x.webp?marker=-2.1940256%2C53.9229587%7Chome-property-pin%402x.png%7Cscale%3A0.5"
            }
          ]
        }
      }
    },
    "__N_SSP": true,
    "page": "/property/uprn/[uprn]",
    "query": {
      "uprn": "61019296"
    },
    "buildId": "qkeVHjsjKJkHtFt7X0D5w",
    "assetPrefix": "https://cdn.prod.zoopla.co.uk",
    "isFallback": false,
    "dynamicIds": [
      6573899826
    ],
    "gssp": true,
    "customServer": true,
    "scriptLoader": []
  }
}
'

enter image description here


Solution

  • Multiline text has to be in ''' or """ (3 quotes).

    And next you have to use data = json.loads(text) to convert it to dictionary.

    And later data["props"]["pageProps"]["property"]["address"]["latitude"] works.


    Full working code:

    text = '''{
      "props": {
        "pageProps": {
          "property": {
            "uprn": "61019296",
            "address": {
              "uprn": "61019296",
              "fullAddress": "8 Arthur Street Barnoldswick BB18 5JZ",
              "postcode": "BB18 5JZ",
              "country": "England",
              "outcode": "BB18",
              "latitude": 53.9229587,
              "longitude": -2.1940256
            },
            "attributes": {
              "bathrooms": null,
              "bedrooms": 3,
              "floorAreaSqM": 109,
              "livingRooms": null,
              "tenure": "Freehold",
              "propertyType": "Mid Terrace House",
              "userSubmitted": null
            },
            "energy": {
              "currentEnergyRating": "C"
            },
            "rentEstimate": {
              "lowerPrice": 700,
              "currentPrice": 750,
              "upperPrice": 800
            },
            "saleEstimate": {
              "lowerPrice": 128000,
              "currentPrice": 135000,
              "upperPrice": 141000,
              "confidenceLevel": "HIGH",
              "valueChange": {
                "numericChange": -16000,
                "percentageChange": -10.596026490066226,
                "saleDate": "2024-12-20"
              },
              "ingestedAt": "2025-02-06T14:05:28.374Z"
            },
            "history": {
              "historicListings": [],
              "historicSales": [
                {
                  "date": "2024-12-20",
                  "price": 151000,
                  "percentageChange": 41.1214953271028,
                  "numericChange": 44000
                },
                {
                  "date": "2024-01-10",
                  "price": 107000,
                  "percentageChange": 3.3816425120772946,
                  "numericChange": 3500
                },
                {
                  "date": "2007-01-19",
                  "price": 103500,
                  "percentageChange": 168.8311688311688,
                  "numericChange": 65000
                },
                {
                  "date": "2001-05-25",
                  "price": 38500,
                  "percentageChange": null,
                  "numericChange": null
                }
              ],
              "soldPricesDataSource": "landRegistry"
            },
            "staticMapImage": {
              "devices": {
                "mobile": 0,
                "mobile2x": 1,
                "medium": 2,
                "large": 3
              },
              "urls": [
                {
                  "height": 200,
                  "width": 400,
                  "src": "https://maps.zoopla.co.uk/styles/portal/static/-2.194025653.922958716/400x200.webp?marker=-2.1940256%2C53.9229587%7Chome-property-pin%402x.png%7Cscale%3A0.5"
                },
                {
                  "height": 200,
                  "width": 400,
                  "src": "https://maps.zoopla.co.uk/styles/portal/static/-2.194025653.922958716/400x200@2x.webp?marker=-2.1940256%2C53.9229587%7Chome-property-pin%402x.png%7Cscale%3A0.5"
                },
                {
                  "height": 170,
                  "width": 1024,
                  "src": "https://maps.zoopla.co.uk/styles/portal/static/-2.194025653.922958716/1024x170.webp?marker=-2.1940256%2C53.9229587%7Chome-property-pin%402x.png%7Cscale%3A0.5"
                },
                {
                  "height": 170,
                  "width": 1024,
                  "src": "https://maps.zoopla.co.uk/styles/portal/static/-2.194025653.922958716/1024x170@2x.webp?marker=-2.1940256%2C53.9229587%7Chome-property-pin%402x.png%7Cscale%3A0.5"
                }
              ]
            }
          }
        },
        "__N_SSP": true,
        "page": "/property/uprn/[uprn]",
        "query": {
          "uprn": "61019296"
        },
        "buildId": "qkeVHjsjKJkHtFt7X0D5w",
        "assetPrefix": "https://cdn.prod.zoopla.co.uk",
        "isFallback": false,
        "dynamicIds": [
          6573899826
        ],
        "gssp": true,
        "customServer": true,
        "scriptLoader": []
      }
    }
    '''
    
    import json
    
    data = json.loads(text)
    
    print('lat :', data["props"]["pageProps"]["property"]["address"]["latitude"])
    print('long:', data["props"]["pageProps"]["property"]["address"]["longitude"])
    

    Result:

    lat : 53.9229587
    long: -2.1940256