pablinhoechel
pablinhoechel

Reputation: 103

JSONDecodeError: Expecting ',' delimiter: Python

I am having trouble reading the json file I am supposed to get from the following code:

    
page=requests.get("https://ffcv.es/ncompeticiones/server.php?action=getEquipo&tmp=2019/2020&id=0202644811&cmp=1294", headers=headers)

data=page.json()

The part from the json object where I am getting the error is the following:

"googleMaps":"https://www.google.es/maps/place/Campo+Municipal+"Olimpic"+De+Onda/@39.9657111,-0.2621533,14z/data=!4m8!1m2!2m1!1scampo+de+futbol+calle+cirat+Onda!3m4!1s0x0:0xa61fd64366a15e5f!8m2!3d39.9"

The "Olimpic" seems to be the problem, I didn't create the json file, I just want to get passed this error and parse its content.

Upvotes: 1

Views: 203

Answers (1)

Andrej Kesely
Andrej Kesely

Reputation: 195593

It's broken Json. A quick-and-dirty fix could be find problematic unescaped quotes and replace them:

import re
import json
import requests

headers = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:77.0) Gecko/20100101 Firefox/77.0'}
page=requests.get("https://ffcv.es/ncompeticiones/server.php?action=getEquipo&tmp=2019/2020&id=0202644811&cmp=1294", headers=headers)

t = re.sub(r'(?<!\\)"\+', r'\\"+', re.sub(r'\+"', r'+\"', page.text))
data = json.loads(t)

# print json back to screen:
print(json.dumps(data, indent=4))

Prints:

{
    "id": "0202644811",
    "nombreEquipo": "C.D. Villa de Onda",
    "idClub": "0202644",
    "nombreClub": "Club Deportivo Villa de Onda",
    "urlFoto": "./img/logosClubes/0202644.jpg",
    "idCompeticion": "1294",
    "competicion": "1\u00aa. Regional G\u00ba. 1",
    "telefono": "627385404",
    "usaCalendarioOficial": "No",
    "diaDeseado": "S&aacute;bado",
    "horaDeseada": "17:00",
    "camiseta": "Blanco",
    "camiseta2": "",
    "tipocamiseta": "Lisa",
    "pantalon": "Negro",
    "camiseta1Hex": "#FFFFFF",
    "camiseta2Hex": "",
    "pantalonHex": "#000000",
    "mediasTexto": "Blanco",
    "mediasHex": "#FFFFFF",
    "estadioPrincipal": {
        "id": "020264401",
        "nombre": "Campo Mpal. \"Olimpic\"  F-11",
        "direccion": "s/n, Cirat, Onda, Castell\u00f3n, 12200",
        "urlFoto": "",
        "tipoLuz": "Artificial poca",
        "tipoSuperficie": "Artificial",
        "googleMaps": "https://www.google.es/maps/place/Campo+Municipal+\"Olimpic\"+De+Onda/@39.9657111,-0.2621533,14z/data=!4m8!1m2!2m1!1scampo+de+futbol+calle+cirat+Onda!3m4!1s0x0:0xa61fd64366a15e5f!8m2!3d39.9"
    },

...

Upvotes: 1

Related Questions