dirksen
dirksen

Reputation: 173

How to output json using HTSQL's python library

From the example:

>>> from htsql import HTSQL
>>> htsql = HTSQL("pgsql:///htsql_demo")
>>> rows = htsql.produce("/school{name, count(department)}")

How do I convert rows into JSON? Using the JSON formatter blows up:

>>> rows = htsql.produce("/school{name, count(department)}/:json")
UnsupportedActionError: unsupported action
While processing:
    /school{name, count(department)}/:json
                                      ^^^^

I'm using HTSQL 2.3.3

Upvotes: 3

Views: 134

Answers (1)

dirksen
dirksen

Reputation: 173

It has to be done via internal API:

from htsql import HTSQL
demo = HTSQL('pgsql:///htsql_demo')
rows = demo.produce('/school{name, count(department)}')

from htsql.core.fmt.emit import emit
with demo:
    text = ''.join(emit('x-htsql/json', rows))

print text

The credit goes to Kirill Simonov, from the HTSQL user's group.

Upvotes: 3

Related Questions