Class SimpleJsonVTI

  • All Implemented Interfaces:
    AutoCloseable, ResultSet, Wrapper, AwareVTI

    public class SimpleJsonVTI
    extends VTITemplate

    This is a table function which turns a JSON array into a relational ResultSet. This table function relies on the JSON.simple JSONArray class found at https://code.google.com/p/json-simple/. Each object in the array is turned into a row. The shape of the row is declared by the CREATE FUNCTION ddl and the shape corresponds to the key names found in the row objects. Provided that the values in those objects have the expected type, the following ResultSet accessors can be called:

    • getString()
    • getBoolean()
    • getByte()
    • getShort()
    • getInt()
    • getLong()
    • getFloat()
    • getDouble()
    • getObject()
    • getBigDecimal()

    This table function relies on the JSONArray type loaded by the simpleJson optional tool. This table function can be combined with other JSONArray-creating functions provided by that tool.

    Here's an example of how to use this VTI on a JSON document read across the network using the readArrayFromURL function provided by the simpleJson tool:

     call syscs_util.syscs_register_tool( 'simpleJson', true );
    
     create function thermostatReadings( jsonDocument JSONArray )
     returns table
     (
       "id" int,
       "temperature" float,
       "fanOn" boolean
     )
     language java parameter style derby_jdbc_result_set contains sql
     external name 'org.apache.derby.optional.api.SimpleJsonVTI.readArray';
     
     select * from table
     (
        thermostatReadings
        (
           readArrayFromURL( 'https://thermostat.feed.org', 'UTF-8' )
        )
     ) t;
     

    That returns a table like this:

     id         |temperature             |fanOn
     ------------------------------------------
     1          |70.3                    |true 
     2          |65.5                    |false
     

    Here's an example of how to use this VTI on a JSON document string with the assistance of the readArrayFromString function provided by the simpleJson tool:

     select * from table
     (
        thermostatReadings
        (
           readArrayFromString
           (
            '[ { "id": 1, "temperature": 70.3, "fanOn": true }, { "id": 2, "temperature": 65.5, "fanOn": false } ]'
           )
        )
     ) t;