4.2. Querying JSON Data in VoltDB

Documentation

Home » Documentation » Performance Guide

4.2. Querying JSON Data in VoltDB

The VoltDB FIELD() column function helps you interact with JSON encoded data. Using the user_session_table schema above, let's assume the table is populated with rows similar to the following:

SELECT username, json_data FROM user_session_table 
    ORDER BY username LIMIT 10

USERNAME    JSON_DATA                         
----------- -------------------------------------------------------------
user-1      {"read_only_user":true,"site":"VoltDB Management","props":
            {"last-login":"1356537244991"}}           
user-10     {"role":"reader","site":"VoltDB Blog","props":{"last-login":
            "1356537252380"}}                       
user-1000   {"role":"reader","site":"VoltDB Blog","props":{"last-login":
            "1356537251017"}}                       
user-10000  {"read_only_user":false,"site":"VoltDB Management","props":
            {"last-login":"1356537246249"}}          
user-10002  {"role":"reader","site":"VoltDB Blog","props":{"last-login":
            "1356537250566"}}                       
user-10003  {"read_only_user":false,"site":"VoltDB Management","props":
            {"last-login":"1356537252187"}}          
user-10004  {"moderator":false,"download_count":0,"site":"VoltDB Forum",
            "props":{"last-login":"1356537244170"}} 
user-10005  {"moderator":false,"download_count":0,"site":"VoltDB Forum",
            "props":{"last-login":"1356537250381"}} 
user-10006  {"moderator":false,"download_count":0,"site":"VoltDB Forum",
            "props":{"last-login":"1356537245804"}} 
user-10009  {"moderator":false,"download_count":0,"site":"VoltDB Forum",
            "props":{"last-login":"1356537249792"}} 

By using the FIELD() function, a query can return only those rows where the login session is from the “VoltDB Forum”. The query and results would look as follows:

SELECT username, json_data FROM user_session_table 
    WHERE field(json_data, 'site')='VoltDB Forum' 
    ORDER BY username LIMIT 10

USERNAME     JSON_DATA                  
-----------  ----------------------------------------------------------------------
user-10004   {"moderator":false,"download_count":0,"site":"VoltDB Forum","props":
             {"last-login":"1356537244170"}}                           
user-10005   {"moderator":false,"download_count":0,"site":"VoltDB Forum","props":
             {"last-login":"1356537250381"}}                           
user-10006   {"moderator":false,"download_count":0,"site":"VoltDB Forum","props":
             {"last-login":"1356537245804"}}                           
user-10009   {"moderator":false,"download_count":0,"site":"VoltDB Forum","props":
             {"last-login":"1356537249792"}}                           
user-10013   {"moderator":false,"download_count":0,"site":"VoltDB  Forum","props":
             {"last-login":"1356537250681","client_language":"Java"}}  
user-10014   {"moderator":false,"download_count":1,"site":"VoltDB Forum","props":
             {"last-login":"1356537251345","download_version":"v2.7"}} 
user-10015   {"moderator":false,"download_count":0,"site":"VoltDB Forum","props":
             {"last-login":"1356537250817"}}                           
user-10016   {"moderator":false,"download_count":0,"site":"VoltDB Forum","props":
             {"last-login":"1356537244761"}}                           
user-10017   {"moderator":false,"download_count":1,"site":"VoltDB Forum","props":
             {"last-login":"1356537253137","download_version":"v3.0"}} 
user-10027   {"moderator":false,"download_count":0,"site":"VoltDB Forum","props":
             {"last-login":"1356537248096","client_language":"Java"}}  

Note that the FIELD() function assumes that the VARChAR column value is valid JSON. If the value is not valid JSON, the query fails with an appropriate error message.

Say you want to refine the result even further and find those Forum sessions that had downloaded any 2.x version content. You can use nested FIELD() function invocations to drill deeper into the JSON structure. For example, the following query fetches the properties for the VoltDB Forum session and then further extracts the download_version field, ultimately pattern matching on the value using the SQL LIKE clause:

SELECT username, json_data FROM user_session_table
    WHERE field(field(json_data, 'props'), 'download_version') 
           LIKE 'v2%' ORDER BY username LIMIT 10

USERNAME    JSON_DATA 
-----------          -------------------------------------------------------
user-10014  {"moderator":false,"download_count":1,"site":"VoltDB Forum",
            "props":{"last-login":"1356537251345","download_version":"v2.7"}}  
user-10030  {"moderator":false,"download_count":1,"site":"VoltDB Forum",
            "props":{"last-login":"1356537250413","download_version":"v2.7"}}  
user-10052  {"moderator":false,"download_count":1,"site":"VoltDB Forum",
            "props":{"last-login":"1356537250274","download_version":"v2.7"}}
user-10087  {"moderator":false,"download_count":1,"site":"VoltDB Forum",
            "props":{"last-login":"1356537247453","download_version":"v2.7"}}
user-10103  {"moderator":false,"download_count":1,"site":"VoltDB Forum",
            "props":{"last-login":"1356537247822","download_version":"v2.7",
            "client_language":"Java"}} 
user-10170  {"moderator":false,"download_count":1,"site":"VoltDB Forum",
            "props":{"last-login":"1356537250308","download_version":"v2.7"}}
user-1018   {"moderator":false,"download_count":1,"site":"VoltDB Forum",
            "props":{"last-login":"1356537252219","download_version":"v2.7"}}
user-10223  {"moderator":false,"download_count":1,"site":"VoltDB Forum",
            "props":{"last-login":"1356537248629","download_version":"v2.7"}}
user-10226  {"moderator":false,"download_count":1,"site":"VoltDB Forum",
            "props":{"last-login":"1356537249328","download_version":"v2.7",
            "client_language":"Java"}} 
user-10227  {"moderator":false,"download_count":1,"site":"VoltDB Forum",
            "props":{"last-login":"1356537252425","download_version":"v2.7"}}