maxlath / wikibase-cli

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

JSON parsing error only when piping to jq #155

Closed miroli closed 2 years ago

miroli commented 2 years ago

Given _querytemplate.js

module.exports = function (positionId) {
    return `
    SELECT ?item ?startdate ?enddate WHERE {
        ?item wdt:P31 wd:Q5;
              p:P39 ?posStatement .

        ?posStatement ps:P39/wdt:P279* wd:${positionId};
                      pq:P580 ?startdate.
        OPTIONAL { ?posStatement pq:P582 ?enddate }
    }`
}

This outputs the expected results: wd sparql query_template.js Q18912794. This saves the expected results to a file: wd sparql query_template.js Q18912794 > results.json. However, this does not work: wd sparql query_template.js Q18912794 | jq -c '.' It complains that the JSON stream is corrupted: parse error: Unfinished JSON term at EOF at line 2974, column 10.

Even stranger, writing the output to a file, and then piping it to jq also works as expected.

maxlath commented 2 years ago

Weird, that was not doing that before, maybe a change I missed in NodeJS handling of process.stdout.write: now (after 92830e5) the process waits for the write to be done before exiting. Patch published in v15.16.1, can you give it a try?

miroli commented 2 years ago

Indeed that did the trick. Thanks for the quick response!