Chioma Okoroafor
Chioma Okoroafor

Reputation: 77

Why do I get "Expecting ',' delimiter:" when accessing a string in a nested dictionary?

I am trying to access the latitude property from a nested JSON file. I tried the code below:

latitude = data["props"]["pageProps"]["property"]["address"]["latitude"]

My JSON:

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/[email protected]?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/[email protected]?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": []
  }
}
'

but I got an error.

I also tried loading it via data_loaded = json.loads(data), but got the error in the picture attached below:

enter image description here

The error:

Expecting ',' delimiter: line 1 column 53 (char 52)

Upvotes: 0

Views: 57

Answers (1)

furas
furas

Reputation: 142985

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/[email protected]?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/[email protected]?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

Upvotes: 1

Related Questions