How to get JSON output
By default zeeSQL returns nested arrays.
zeeSQL can also returns the exact same information as JSON output.
JSON output may be preferable since it is usually easier to parse and all languages offer full support for it.
The ZEESQL.EXEC and ZEESQL.QUERY commands support the JSON flag, which instructs them to return JSON as output.

Examples

The first example is about a command that does not return any rows but only DONE.
1
127.0.0.1:6379> ZEESQL.CREATE_DB DB
2
1) 1) "OK"
3
127.0.0.1:6379> ZEESQL.EXEC DB COMMAND "CREATE TABLE foo(col1 STRING, col2 INT, col3 STRING);"
4
1) 1) "DONE"
5
2) 1) (integer) 0
6
127.0.0.1:6379> ZEESQL.EXEC DB COMMAND "CREATE TABLE bar(col1 STRING, col2 INT, col3 STRING);" JSON
7
"{\"result\":\"done\",\"modified_rows\":0}"
Copied!
Adding the JSON flags returns the exact same result but in JSON format.
1
127.0.0.1:6379> ZEESQL.EXEC DB COMMAND "INSERT INTO foo VALUES('AAA', 2, 'BBB'),('CCC', 3, 'DDD');"
2
1) 1) "DONE"
3
2) 1) (integer) 2
4
127.0.0.1:6379> ZEESQL.EXEC DB COMMAND "INSERT INTO bar VALUES('AAA', 2, 'BBB'),('CCC', 3, 'DDD');" JSON
5
"{\"result\":\"done\",\"modified_rows\":2}"
Copied!
Another example is when the command returns some rows.
1
127.0.0.1:6379> ZEESQL.EXEC DB COMMAND "SELECT * FROM foo;"
2
1) 1) "RESULT"
3
2) 1) "col1"
4
2) "col2"
5
3) "col3"
6
3) 1) "TEXT"
7
2) "INT"
8
3) "TEXT"
9
4) 1) "AAA"
10
2) (integer) 2
11
3) "BBB"
12
5) 1) "CCC"
13
2) (integer) 3
14
3) "DDD"
15
127.0.0.1:6379> ZEESQL.EXEC DB COMMAND "SELECT * FROM bar;" JSON
16
"{\"rows\":[{\"col1\":\"AAA\",\"col2\":2,\"col3\":\"BBB\"},{\"col1\":\"CCC\",\"col2\":3,\"col3\":\"DDD\"}],\"number_of_rows\":2,\"columns\":{\"col1\":\"TEXT\",\"col2\":\"INT\",\"col3\":\"TEXT\"}}"
Copied!
The returned JSON is formatted for saving bytes on the network, not for readability.
However, the result looks like this:
1
{
2
"rows": [
3
{
4
"col1": "AAA",
5
"col2": 2,
6
"col3": "BBB"
7
},
8
{
9
"col1": "CCC",
10
"col2": 3,
11
"col3": "DDD"
12
}
13
],
14
"number_of_rows": 2,
15
"columns": {
16
"col1": "TEXT",
17
"col2": "INT",
18
"col3": "TEXT"
19
}
20
}
Copied!
The rows key contains the actual result set, each row is an object with a key the name of the column and with value the actual value of the row.
Then there is the number_of_rows key, an integer that describes how many rows are returned in this result set.
The columns field maps the name of the column to their type.
Overall returning JSON from zeeSQL is very simple, just add the JSON flag to your command and you are done.

About zeeSQL

zeeSQL is a Redis Module that provides SQL capabilities to Redis. It allows the creation and management of several SQL databases, each one independent from the other. Moreover, zeeSQL provides out-of-the-box secondary indexes capabilities, allowing fast and easy search by value in Redis.
Last modified 7mo ago
Copy link