KSQLDB Rest API: return query results as json

1.4k views Asked by At

I'm issuing the following request:

curl --http2 -X "POST" "http://localhost:8088/query-stream"
             -H "Content-Type: application/vnd.ksql.v1+json; charset=utf-8"
             -d $'{
                  "sql": "SELECT * FROM USERS EMIT CHANGES;",
                  "streamsProperties": {
                    "ksql.streams.auto.offset.reset": "earliest"
                  }
}

The result I'm getting is:

{"queryId":"cdfb3ccc-0ab5-4186-a249-b279bfc09587","columnNames":["USERID","NAME"],"columnTypes":["STRING","STRING"]}
["1","Max"]
["2","Alex"]
["13","Andrew"]
...

Is there a way I could get the data in json format?

{"userid":"1","name":"Max"}
{"userid":"2","name":"Alex"}
{"userid":"13","name":"Andrew"}

It is easier to deserialize this data into POCO objects if they are in json than to parse the 'row' format.

1

There are 1 answers

2
Robin Moffatt On

Per the docs you can set the Accept header. It defaults to application/vnd.ksqlapi.delimited.v1 but can also be set to application/json:

curl --show-error --silent \
         -H "application/vnd.ksqlapi.delimited.v1" \
         --http2 'http://localhost:8088/query-stream' \
         --data-raw '{"sql":"SELECT * FROM CUSTOMERS WHERE ID=42;"}'
{"queryId":null,"columnNames":["ID","FIRST_NAME","LAST_NAME","EMAIL","GENDER","COMMENTS"],"columnTypes":["INTEGER","STRING","STRING","STRING","STRING","STRING"]}
[42,"Rick","Astley","[email protected]","Male",""]
curl --show-error --silent \
         -H "Accept:application/json" \
         --http2 'http://localhost:8088/query-stream' \
         --data-raw '{"sql":"SELECT * FROM CUSTOMERS WHERE ID=42;"}'
[{"queryId":null,"columnNames":["ID","FIRST_NAME","LAST_NAME","EMAIL","GENDER","COMMENTS"],"columnTypes":["INTEGER","STRING","STRING","STRING","STRING","STRING"]},[42,"Rick","Astley","[email protected]","Male",""]]