Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to output json using HTSQL's python library

Tags:

htsql

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

like image 988
dirksen Avatar asked May 29 '26 00:05

dirksen


1 Answers

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.

like image 172
dirksen Avatar answered Jun 02 '26 18:06

dirksen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!