Formatting query results as JSON
Use the SELECT command to return JSON data.
Use CQL SELECT keywords to retrieve data from a table in the JSON format. For more information, see What's New in Cassandra 2.2: JSON Support.
Retrieving all results in the JSON format
To get this result, insert the JSON keyword between the
SELECT command and the data specifications. For
example:
cqlsh:cycling> select json name, checkin_id, timestamp from checkin;
[json]<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE map PUBLIC "-//OASIS//DTD DITA Map//EN" "map.dtd">
<map>
<title>meta</title>
<mapref href="dse-wip.ditamap"/>
<mapref href="install.ditamap"/>
<mapref href="security.ditamap"/>
<mapref href="cql.ditamap"/>
<mapref href="opscuserguide.ditamap"/>
<mapref href="studio.ditamap"/>
<topicref href="variables.ditamap" format="ditamap"/>
<topicref href="universalCommons.ditamap" format="ditamap"/>
</map>
------------------------------------------------------------------------------------------------------------------
{"name": "BRAND", "checkin_id": "50554d6e-29bb-11e5-b345-feff8194dc9f", "timestamp": "2016-08-28 21:45:10.406Z"}
{"name": "VOSS", "checkin_id": "50554d6e-29bb-11e5-b345-feff819cdc9f", "timestamp": "2016-08-28 21:44:04.113Z"}
(2 rows)
Retrieving selected columns in JSON format
To specify the JSON format for a selected column, enclose its name in
toJson()
. For
example:cqlsh:cycling> select name, checkin_id, toJson(timestamp) from checkin;
name | checkin_id | system.tojson(timestamp)
-------+--------------------------------------+----------------------------
BRAND | 50554d6e-29bb-11e5-b345-feff8194dc9f | "2016-08-28 21:45:10.406Z"
VOSS | 50554d6e-29bb-11e5-b345-feff819cdc9f | "2016-08-28 21:44:04.113Z"
Note: DataStax Enterprise supports returning a JSON-formatted
timestamp
with complete time zone information.