r_mapjson(C;S;X;O)

Returns a string containing a JSON representation of a package for each row in a table. (Available as of version 15.21)

Function type

Vector only

Syntax

r_mapjson(C;S;X;O)

Input

Argument Type Description
C any A space- or comma-separated list of column names

C may also be a string expression (in single quotes) constituting a pattern for matching columns. For instance, 'foo*' specifies all columns that begin with "foo".

If C is omitted, the values in all of the columns are included.

Note: The columns specified by C must be either all numeric (integer and decimal) or all text.
S integer A boolean column or an expression in rcv_/rcn_/rct_ which determines whether or not that column is selected to be included in the calculation. See System variables for more information about rcv_/rcn_/rct_.

If S is omitted, all columns will be considered by the function (subject to any prior column selections).

X any A column name
X is an expression that is used to transform the value or name of each of the selected columns.
Note: Use the system variables rcv_, rcn_, and rct_ to reference the column value, name, and type, respectively.
O text A list of options as accepted by the function enjson(V;O).

These options are as follows:

  • 'noquotekeys' leaves out the double-quote characters surroundings the keys in object literals.
  • 'quotekeys' surrounds the keys with double-quote characters in object literals. This is the default.

Return Value

For each row of the table, r_mapjson(C;S;X;O) returns a JSON representation of the package value of a row. The keys of the package are the column names specified in C. The values of the package corresponding to the keys are the values at each column of X, an expression in rcv_/rcn_/rct_. If X is omitted, then the values are simply the values of each column (or rcv_, the default). For example, r_mapjson(;;;) yields a JSON representation of a package whose keys are the column names and whose values are the values of those columns at each row.

Note: Not all data types can be represented, or represented with full precision, in JSON.

Example

The following code finds the columns names beginning with the letter 'd' in the table pub.demo.retail.prod, selects only those columns that contain string data, converts the values contained within the columns to upper case, and returns the JSON representation of the resulting package:

<base table="pub.demo.retail.prod"/>
<willbe name="json" value="r_mapjson('d*';rct_='a';strupcase(rcv_);)"/>
<colord cols="json"/>

The C argument, 'd*', tells r_mapjson to select only those column names starting with 'd'. The S argument, rct_='a', tells r_mapjson to select only columns of type string. The only columns beginning with 'd' and containing string data are deptdesc and divdesc. The expression in X,strupcase(rcv_), tells r_mapjson to convert the values in each column to upper case. Finally, r_mapjson returns a JSON representation of the package, using 'quotekeys' (the default).