Returning csv from the wikidata query api

The info is here: https://www.mediawiki.org/wiki/Wikidata_Query_Service/User_Manual#Supported_formats

It's NOT possible to append "format=csv" to the query string.


curl "https://query.wikidata.org/sparql?query=SELECT%20%3Fitem%20%3FitemLabel%20%0AWHERE%20%0A%7B%0A%20%20%3Fitem%20wdt%3AP31%20wd%3AQ146.%0A%20%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22en%2Cen%22.%20%7D%0A%7D%0A&format=csv" -o cats.csv

This will return the default format, xml, and who wants that these days?

To get csv, you have to send the "Accept: text/csv" header and no format param, like so, using curl for example:


curl https://query.wikidata.org/sparql?query=SELECT%20%3Fitem%20%3FitemLabel%20%0AWHERE%20%0A%7B%0A%20%20%3Fitem%20wdt%3AP31%20wd%3AQ146.%0A%20%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22en%2Cen%22.%20%7D%0A%7D%0A -H "Accept: text/csv" -o cats.csv

Hope it helps.

Posted: 22 March 2021

comments powered by Disqus