maxlath / wikibase-cli

read and edit a Wikibase instance from the command line
MIT License
223 stars 24 forks source link

wd sparql json output not readable by json.decoder #111

Closed rwst closed 4 years ago

rwst commented 4 years ago

It seems when the query results in a list of single values or items the output makes json.decoder bail out which is unexpected. With the following in foo.rq

SELECT ?item
{
  ?item wdt:P31 wd:Q81408532.
  ?item wdt:P31 wd:Q898273
}

the command wd sparql foo.rq >foo.json gets this in foo.json: Q28544143 Q28544154 Q28544157 Q28544178 Q28544256 Q28544266 Q28544275 Q28544277 Q28544287 Q28544304 Q28544320 Q32855755 Q32855866 Q41796953 Q41797023 Q41797024 Q43921422 Q43921543 Q24721339 Q24723238 Q24723837 Q24724760 Q24726447 Q24726464 and this code normally would read it:

file = open('foo.json')
s = file.read()
jol = json.loads(s)

but results in:

Traceback (most recent call last):
  File "sync-ipr-domain-fams.py", line 32, in <module>
    jol = json.loads(s)
  File "/usr/lib64/python3.6/json/__init__.py", line 354, in loads
    return _default_decoder.decode(s)
  File "/usr/lib64/python3.6/json/decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib64/python3.6/json/decoder.py", line 357, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
maxlath commented 4 years ago

yes, that space-separated list of values can be quite useful in some cases, that's thus the default when there is only one variable. To consistently get JSON, you can add --json