BuildRowSetFromJson()
Loads JSON data into a rowset. Use this function if you have JSON data that you want to programmatically insert into your content. This function is executed at send time for outgoing messages and at load time for CloudPages.
The BuildRowSetFromJson()
function has three parameters:
jsonData
(string): Required. The JSON data that you want to parse.jsonPathExpression
(string): Required. The JSONPath expression that parses the source data.boolReturnEmptyOnError
(boolean): Required. Iffalse
, the function returns an empty rowset when there’s a syntax error in the function or the JSONPath expression. Iftrue
, the function returns an exception when an error occurs.
The value of the jsonData
parameter can’t be a simple JSON object such as {"first": "John", "last": "Smith", "country": "US"}
.
Pass a valid JSONPath expression for the jsonPathExpression
parameter. The JSONPath expression that you specify can use bracket notation or dot notation. This function doesn’t support JSONPath filter expressions.
If the JSONPath expression produces values, the rowset contains a Value
column with the value of the field that you queried.
If the JSONPath expression produces objects, the fields of those objects are returned as columns of the rowset. The values of those columns are the values in that object. Exceptions to this rule occur when the value is an object or array type. In these cases, the value is JSON Object
or JSON Object Array
, respectively.
Consider a JSON object that contains this data.
To obtain the objects in the Flights
array using AMPscript, use the BuildRowsetFromJson()
function.
When your content includes this code, the system returns a rowset that contains this data.
Origin | Dest | Price | PerBagSurcharge |
---|---|---|---|
IND | NYC | 100 | |
IND | LAX | 200 | |
IND | SEA | 500 | 25 |
If you only need the content in a specific column of the rowset, modify the JSONPath expression to refer to the key name that corresponds to that row. For example, if you only want the price data, use this AMPscript code.
When your content includes this code, the system returns a rowset that contains this data.
Value |
---|
100 |
200 |
500 |
If the function contains a syntax error, it returns an exception. A few circumstances result in an exception.
- The function doesn’t include the required number of parameters.
- The value of the first parameter contains JSON data with invalid syntax and the value of the third parameter is
0
orfalse
. - The value of the second parameter contains a JSONPath argument with invalid syntax and the value of the third parameter is
0
orfalse
. - The value of the third parameter contains anything other than a number or boolean.
If the JSONPath expression doesn’t produce any data, the function returns an empty row. The function returns an empty rowset in these situations.
- The JSONPath expression is valid but refers to an object that doesn’t exist in the source data.
- The JSONPath expression is valid, but the JSON source data is invalid, and the value of the third parameter is
1
ortrue
.
To handle cases in which the function returns empty rows, you can use the RowCount
function to check the number of rows that the BuildRowSetFromJson()
function returns. You can then use If
and Else
statements to display a fallback message when the function returns an empty rowset. The code example in the next section shows how to handle this situation.
You can use the BuildRowSetFromJson()
function to parse JSON data and insert the data that it contains into your HTML. This example performs some basic validation and formatting to handle situations where the input data is inconsistent. It also checks to make sure that the value that the BuildRowSetFromJson()
function provides isn’t empty and prints an error message if it’s empty. Finally, the code creates an HTML table using the JSON data.
The rendered HTML contains a table that resembles this example.
Origin | Destination | Price | Per bag Surcharge |
---|---|---|---|
IND | NYC | $100.00 | $0.00 |
IND | LAX | $200.00 | $0.00 |
IND | SEA | $500.00 | $25.00 |