0

trying to wrap a working query into the python code to avoid the time-out issue, but get the error JSONDecodeError: Expecting value: line 1 column 1 (char 0) could someone check what's wrong in there? I feel like I have to catch the exception somewhere but not sure how:

     #! /usr/bin/env python3

from qwikidata.sparql import return_sparql_query_results
import json

data = return_sparql_query_results("""
SELECT DISTINCT ?poi ?itemLabel_es ?itemLabel_en ?itemLabel_ceb ?itemLabel_ca ?itemLabel_gl ?itemLabel_eu ?itemLabel_ru ?itemLabel_uk ?typeLabel ?localizationLabel WHERE {
#here change: wd:Q for country
  ?poi (p:P17/ps:P17) wd:Q29.
#here change: adapt all UNION statements, remove which not needed, change wd:Q for feature types for UNION statements
{         ?poi p:P31/ps:P31/wdt:P279* wd:Q10354598.       }      
 UNION       {         ?poi p:P31/ps:P31/wdt:P279* wd:Q532.       } 
 UNION       {         ?poi p:P31/ps:P31/wdt:P279* wd:Q4632675.       } 
 UNION       {         ?poi p:P31/ps:P31/wdt:P279* wd:Q699405.       } 
 UNION       {         ?poi p:P31/ps:P31/wdt:P279* wd:Q5084.       }
#here change: adapt all language lables, remove which not needed, language code can be taken from wikidata  
 OPTIONAL {
    ?poi rdfs:label ?itemLabel_es.
    FILTER((LANG(?itemLabel_es)) = "es")
  }
  OPTIONAL {
    ?poi rdfs:label ?itemLabel_en.
    FILTER((LANG(?itemLabel_en)) = "en")
  }
  OPTIONAL {
    ?poi rdfs:label ?itemLabel_ceb.
    FILTER((LANG(?itemLabel_ceb)) = "ceb")
  }
    OPTIONAL {
    ?poi rdfs:label ?itemLabel_ca.
    FILTER((LANG(?itemLabel_ca)) = "ca")
  }
      OPTIONAL {
    ?poi rdfs:label ?itemLabel_gl.
    FILTER((LANG(?itemLabel_gl)) = "gl")
  }
        OPTIONAL {
    ?poi rdfs:label ?itemLabel_eu.
    FILTER((LANG(?itemLabel_eu)) = "eu")
  }
    OPTIONAL {
    ?poi rdfs:label ?itemLabel_ru.
    FILTER((LANG(?itemLabel_ru)) = "ru")
  }
      OPTIONAL {
    ?poi rdfs:label ?itemLabel_uk.
    FILTER((LANG(?itemLabel_uk)) = "uk")
  }
   OPTIONAL{
      ?poi p:P31/ps:P31 ?type  
  SERVICE wikibase:label { ?type rdfs:label ?typeLabel . bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } }
   OPTIONAL{
      ?poi p:P131/ps:P131 ?localization  
  SERVICE wikibase:label { ?localization rdfs:label ?localizationLabel . bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } }
  }
Limit 50000
""")
 encodedUnicode  =  json.dumps(data, ensure_ascii=False)
 decodingJSON = json.loads(encodedUnicode, strict=False)
 with open ('data_smple.txt', 'w') as target_file:
     target_file.write(json.dumps(decodingJSON, indent=4))

print(repr(data[24257164-20:24257164+20]))
Elart
  • 1
  • 2
  • 1
    Does this answer your question? [JSONDecodeError: Expecting value: line 1 column 1 (char 0)](https://stackoverflow.com/questions/16573332/jsondecodeerror-expecting-value-line-1-column-1-char-0) – Ulrich Eckhardt Aug 24 '22 at 09:50
  • Please, first search for the error message next time. Lots of similar problems and solutions to find. Also, read [ask] and take the [tour]. – Ulrich Eckhardt Aug 24 '22 at 09:51
  • @UlrichEckhardt thanks for your input. checked the answer before the question have been asked, and tried several options, not working for me though, I'm missing something I guess, but keeping the init code as clean as I can not to mess up the commenters – Elart Aug 24 '22 at 09:55
  • please post the self-contained Python code which leads to the error – UninformedUser Aug 24 '22 at 11:29
  • sorry for being away for a while, got back to the stepping stone, have added the code which shows the decoding issue, and tried to solve it with decodingJSON = json.loads(encodedUnicode, strict=False), doesn't seem that it helped thogh -_- . Any ideas @UninformedUser, @UlrichEckhardt? – Elart Sep 07 '22 at 08:56

0 Answers0