VoltDB Home » Documentation » Using VoltDB


SET_FIELD() — Returns a copy of a JSON-encoded string, replacing the specified field value.


SET_FIELD( column, field-name-path, string-value )


The SET_FIELD() function finds the specified field within a JSON-encoded string and returns a copy of the string with the new value replacing that field's previous value. Note that the SET_FIELD() function returns an altered copy of the JSON-encoded string — it does not change any column values in place. So to change existing database columns, you must use SET_FIELD() with an UPDATE statement.

For example, assume the Product table contains a VARCHAR column Productinfo which for one row contains the following JSON string:

{"product":"Acme widget",
 "info": { "description": "A fancy widget.",

It is possible to change the value of the availability field using the SET_FIELD function, like so:

UPDATE Product SET Productinfo = 
    WHERE FIELD(Productinfo,'product') = 'Acme widget';

The second argument to the SET_FIELD() function can be a simple field name, as in the previous example, In which case the function replaces the value of the top field matching the specified name. Alternately, you can specify a path representing a hierarchy of names separated by periods. For example, you can replace the SKU number by specifying "info.sku" as the second argument, or you can replace the number of units in the second warehouse by specifying the field path "warehouse[1].units". For example, the following UPDATE statement does both by nesting SET_FIELD commands:

UPDATE Product SET Productinfo = 
          'warehouse[1].units', '128')
    WHERE FIELD(Productinfo,'product') = 'Acme widget';

Note that the third argument is the string value that will be inserted into the JSON-encoded string. To insert a numeric value, you enclose the value in single quotation marks, as in the preceding example where '128' is used as the replacement value for the warehouse[1].units field. To insert a string value, you must include the string quotation marks within the replacement string itself. For example, the preceding code uses the SQL string constant '"DEFGHI"' to specify the replacement value for the text field info.sku.

Finally, the replacement string value can be any valid JSON value, including another JSON-encoded object or array. It does not have to be a scalar string or numeric value.


The following example uses the SET_FIELD() function to add a new array element to the warehouse field.

UPDATE Product SET Productinfo = 
    WHERE FIELD(Productinfo,'product') = 'Acme widget';