Runtime verified Differs from official docs Test scripts included

Syntax

BuildRowsetFromJSON(jsonData, jsonPathExpression, returnEmptyOnError)  →  rowset
3 arguments — exactly

Parameters

Name Type Required Description
jsonData string Yes The JSON payload to parse
jsonPathExpression string Yes JSONPath expression selecting the nodes to turn into rows
returnEmptyOnError string | boolean | number Yes Error flag. true, false, 1, 0, "true", "false", "1", "0" are all accepted; with valid JSON every one of them returns the same rows

All three arguments are needed: an email that calls the function with only two cannot be saved.

Example

%%[
  VAR @json, @rows
  SET @json = '{"Flights":[{"Origin":"IND","Dest":"NYC","Price":100.0},{"Origin":"IND","Dest":"LAX","Price":200.0},{"Origin":"IND","Dest":"SEA","Price":500.0,"PerBagSurcharge":25}]}'
  SET @rows = BuildRowsetFromJSON(@json, "$.Flights[*]", 1)
]%%
Rows: %%=RowCount(@rows)=%% last: %%=Field(Row(@rows, 3), "Dest")=%% surcharge: %%=Field(Row(@rows, 3), "PerBagSurcharge", 0)=%%

Renders Rows: 3 last: SEA surcharge: 25.

Return value

rowset — one row per node the JSONPath expression matched.

A path that matches nothing, an empty array ("[]" with $[*]) and the text not json all give a rowset of zero rows, so branch on RowCount.

Behaviour

An array of objects gives one row per element. $.Flights[*] over three flights gives three rows. Read columns by the exact key: the first row’s Origin and Dest give IND and NYC. A key that only the third flight carries (PerBagSurcharge) reads 25 there.

Column names are case-sensitive. Reading dest instead of Dest gives an empty string.

Numbers keep their decimals. A Price of 100.0 reads back as 100.0.

Reading by position does not return the value. Field(row, 1) and Field(row, 2) on the first flight give empty strings. On rows built from scalars ($.Flights[*].Price, $.* over a flat object, $.Shapes[*] over a string array) the row count is right, but Field(row, 1) gives true and Field(row, "Value", 0) gives false. Read objects by key name.

A missing column renders empty. Both Field(row, "NoSuchColumn", 0) and the two-argument Field(row, "NoSuchColumn") render an empty string; neither fails the email.

On the payload { not json the flag works the reverse way from Engagement. With the flag 0 the email renders on, where Engagement aborts. An email that used the truthy flags true, "true" and "1" together on that payload failed to render (HTTP 500), where Engagement returns zero rows; which of the three spellings fails was not isolated. On the payload not json, the flag 1 gave zero rows.

Some inputs break the render. An empty string and an unset variable as the payload both made the preview fail. Test @json for content before you call the function.

Either capitalisation works. BuildRowSetFromJSON with a capital S gives the same three rows.

Show Next test script
<!-- Marketing Cloud Next test script for BuildRowsetFromJSON. Paste it into the HTML component of a
     Next email and open the preview. Each line prints the result Next gave when the call
     was rendered on its own (expect) next to the result of this run (got).
     Assembled from single-call test emails (dates on the page); it has not been
     run as one email. Calls that make Next fail are listed separately. -->
<p>c1 expect=[1] got=[%%[ SET @bj1 = BuildRowsetFromJSON('{"Flights":[{"Origin":"IND","Dest":"NYC","Price":100.0}]}', "$.Flights[*]", 1) ]%%%%=RowCount(@bj1)=%%]</p>
<p>c2 expect=[IND] got=[%%[ SET @bj2 = BuildRowsetFromJSON('{"Flights":[{"Origin":"IND","Dest":"NYC","Price":100.0}]}', "$.Flights[*]", 1) ]%%%%=Field(Row(@bj2, 1), "Origin")=%%]</p>
<p>c3 expect=[100.0] got=[%%[ SET @bj3 = BuildRowsetFromJSON('{"Flights":[{"Origin":"IND","Dest":"NYC","Price":100.0}]}', "$.Flights[*]", 1) ]%%%%=Field(Row(@bj3, 1), "Price")=%%]</p>
<p>c4 expect=[0] got=[%%[ SET @bj4 = BuildRowsetFromJSON("not json", "$.Flights[*]", 1) ]%%%%=RowCount(@bj4)=%%]</p>
<p>c5 expect=[3] got=[%%[ SET @r2bj1 = BuildRowsetFromJSON('{"Flights":[{"Origin":"IND","Dest":"NYC","Price":100.0},{"Origin":"IND","Dest":"LAX","Price":200.0},{"Origin":"IND","Dest":"SEA","Price":500.0,"PerBagSurcharge":25}]}', "$.Flights[*]", 1) ]%%%%=RowCount(@r2bj1)=%%]</p>
<p>c6 expect=[IND|NYC|100.0] got=[%%[ SET @r2bj2 = BuildRowsetFromJSON('{"Flights":[{"Origin":"IND","Dest":"NYC","Price":100.0},{"Origin":"IND","Dest":"LAX","Price":200.0},{"Origin":"IND","Dest":"SEA","Price":500.0,"PerBagSurcharge":25}]}', "$.Flights[*]", 1) ]%%%%=Field(Row(@r2bj2,1),"Origin")=%%|%%=Field(Row(@r2bj2,1),"Dest")=%%|%%=Field(Row(@r2bj2,1),"Price")=%%]</p>
<p>c7 expect=[SEA|25] got=[%%[ SET @r2bj3 = BuildRowsetFromJSON('{"Flights":[{"Origin":"IND","Dest":"NYC","Price":100.0},{"Origin":"IND","Dest":"LAX","Price":200.0},{"Origin":"IND","Dest":"SEA","Price":500.0,"PerBagSurcharge":25}]}', "$.Flights[*]", 1) ]%%%%=Field(Row(@r2bj3,3),"Dest")=%%|%%=Field(Row(@r2bj3,3),"PerBagSurcharge",0)=%%]</p>
<p>c8 expect=[] got=[%%[ SET @r2bj4 = BuildRowsetFromJSON('{"Flights":[{"Origin":"IND","Dest":"NYC","Price":100.0},{"Origin":"IND","Dest":"LAX","Price":200.0},{"Origin":"IND","Dest":"SEA","Price":500.0,"PerBagSurcharge":25}]}', "$.Flights[*]", 1) ]%%%%=Field(Row(@r2bj4,3),"dest")=%%]</p>
<p>c9 expect=[|] got=[%%[ SET @r2bj5 = BuildRowsetFromJSON('{"Flights":[{"Origin":"IND","Dest":"NYC","Price":100.0},{"Origin":"IND","Dest":"LAX","Price":200.0},{"Origin":"IND","Dest":"SEA","Price":500.0,"PerBagSurcharge":25}]}', "$.Flights[*]", 1) ]%%%%=Field(Row(@r2bj5,1),1)=%%|%%=Field(Row(@r2bj5,1),2)=%%]</p>
<p>c10 expect=[3|false|true] got=[%%[ SET @r2bj6 = BuildRowsetFromJSON('{"Flights":[{"Origin":"IND","Dest":"NYC","Price":100.0},{"Origin":"IND","Dest":"LAX","Price":200.0},{"Origin":"IND","Dest":"SEA","Price":500.0,"PerBagSurcharge":25}]}', "$.Flights[*].Price", 1) ]%%%%=RowCount(@r2bj6)=%%|%%=Field(Row(@r2bj6,2),"Value",0)=%%|%%=Field(Row(@r2bj6,2),1)=%%]</p>
<p>c11 expect=[2|true|true] got=[%%[ SET @r2bj7 = BuildRowsetFromJSON('{"Id":123,"Shape":"Square"}', "$.*", 1) ]%%%%=RowCount(@r2bj7)=%%|%%=Field(Row(@r2bj7,1),1)=%%|%%=Field(Row(@r2bj7,2),1)=%%]</p>
<p>c12 expect=[3|true|false] got=[%%[ SET @r2bj8 = BuildRowsetFromJSON('{"Shapes":["Triangle","Circle","Square"]}', "$.Shapes[*]", 1) ]%%%%=RowCount(@r2bj8)=%%|%%=Field(Row(@r2bj8,2),1)=%%|%%=Field(Row(@r2bj8,2),"Value",0)=%%]</p>
<p>c13 expect=[3||2|true] got=[%%[ SET @r2bj9 = BuildRowsetFromJSON('{"a":{"b":1},"c":[1,2],"d":"plain"}', "$.*", 1) ]%%%%=RowCount(@r2bj9)=%%|%%=Field(Row(@r2bj9,1),1)=%%|%%=Field(Row(@r2bj9,2),1)=%%|%%=Field(Row(@r2bj9,3),1)=%%]</p>
<p>c14 expect=[0] got=[%%[ SET @r2bj12 = BuildRowsetFromJSON("[]", "$[*]", 1) ]%%%%=RowCount(@r2bj12)=%%]</p>
<p>c15 expect=[0] got=[%%[ SET @r2bj14 = BuildRowsetFromJSON('{"Flights":[{"Origin":"IND","Dest":"NYC","Price":100.0},{"Origin":"IND","Dest":"LAX","Price":200.0},{"Origin":"IND","Dest":"SEA","Price":500.0,"PerBagSurcharge":25}]}', "$.NoSuchKey[*]", 1) ]%%%%=RowCount(@r2bj14)=%%]</p>
<p>c16 expect=[3] got=[%%[ SET @r2bj15 = BuildRowsetFromJSON('{"Flights":[{"Origin":"IND","Dest":"NYC","Price":100.0},{"Origin":"IND","Dest":"LAX","Price":200.0},{"Origin":"IND","Dest":"SEA","Price":500.0,"PerBagSurcharge":25}]}', "$.Flights[*]", true) ]%%%%=RowCount(@r2bj15)=%%]</p>
<p>c17 expect=[3] got=[%%[ SET @r2bj16 = BuildRowSetFromJSON('{"Flights":[{"Origin":"IND","Dest":"NYC","Price":100.0},{"Origin":"IND","Dest":"LAX","Price":200.0},{"Origin":"IND","Dest":"SEA","Price":500.0,"PerBagSurcharge":25}]}', "$.Flights[*]", 1) ]%%%%=RowCount(@r2bj16)=%%]</p>
<p>c18 expect=[[]] got=[%%[ SET @r2bj18 = BuildRowsetFromJSON('{"Flights":[{"Origin":"IND","Dest":"NYC","Price":100.0},{"Origin":"IND","Dest":"LAX","Price":200.0},{"Origin":"IND","Dest":"SEA","Price":500.0,"PerBagSurcharge":25}]}', "$.Flights[*]", 1) ]%%[%%=Field(Row(@r2bj18,1),"NoSuchColumn",0)=%%]]</p>
<p>c19 expect=[3|3|3|3|3|3|3|3] got=[%%[ SET @r2bj20 = BuildRowsetFromJSON('{"Flights":[{"Origin":"IND","Dest":"NYC","Price":100.0},{"Origin":"IND","Dest":"LAX","Price":200.0},{"Origin":"IND","Dest":"SEA","Price":500.0,"PerBagSurcharge":25}]}', "$.Flights[*]", true) SET @r2bj21 = BuildRowsetFromJSON('{"Flights":[{"Origin":"IND","Dest":"NYC","Price":100.0},{"Origin":"IND","Dest":"LAX","Price":200.0},{"Origin":"IND","Dest":"SEA","Price":500.0,"PerBagSurcharge":25}]}', "$.Flights[*]", false) SET @r2bj22 = BuildRowsetFromJSON('{"Flights":[{"Origin":"IND","Dest":"NYC","Price":100.0},{"Origin":"IND","Dest":"LAX","Price":200.0},{"Origin":"IND","Dest":"SEA","Price":500.0,"PerBagSurcharge":25}]}', "$.Flights[*]", 1) SET @r2bj23 = BuildRowsetFromJSON('{"Flights":[{"Origin":"IND","Dest":"NYC","Price":100.0},{"Origin":"IND","Dest":"LAX","Price":200.0},{"Origin":"IND","Dest":"SEA","Price":500.0,"PerBagSurcharge":25}]}', "$.Flights[*]", 0) SET @r2bj24 = BuildRowsetFromJSON('{"Flights":[{"Origin":"IND","Dest":"NYC","Price":100.0},{"Origin":"IND","Dest":"LAX","Price":200.0},{"Origin":"IND","Dest":"SEA","Price":500.0,"PerBagSurcharge":25}]}', "$.Flights[*]", "true") SET @r2bj25 = BuildRowsetFromJSON('{"Flights":[{"Origin":"IND","Dest":"NYC","Price":100.0},{"Origin":"IND","Dest":"LAX","Price":200.0},{"Origin":"IND","Dest":"SEA","Price":500.0,"PerBagSurcharge":25}]}', "$.Flights[*]", "false") SET @r2bj26 = BuildRowsetFromJSON('{"Flights":[{"Origin":"IND","Dest":"NYC","Price":100.0},{"Origin":"IND","Dest":"LAX","Price":200.0},{"Origin":"IND","Dest":"SEA","Price":500.0,"PerBagSurcharge":25}]}', "$.Flights[*]", "1") SET @r2bj27 = BuildRowsetFromJSON('{"Flights":[{"Origin":"IND","Dest":"NYC","Price":100.0},{"Origin":"IND","Dest":"LAX","Price":200.0},{"Origin":"IND","Dest":"SEA","Price":500.0,"PerBagSurcharge":25}]}', "$.Flights[*]", "0") ]%%%%=RowCount(@r2bj20)=%%|%%=RowCount(@r2bj21)=%%|%%=RowCount(@r2bj22)=%%|%%=RowCount(@r2bj23)=%%|%%=RowCount(@r2bj24)=%%|%%=RowCount(@r2bj25)=%%|%%=RowCount(@r2bj26)=%%|%%=RowCount(@r2bj27)=%%]</p>
<p>c20 expect=[X] got=[%%[ SET @r2bj10 = BuildRowsetFromJSON("{ not json ", "$.Flights[*]", 0) ]%%X]</p>
<p>c21 expect=[] got=[%%[ SET @r2bj17 = BuildRowsetFromJSON('{"Flights":[{"Origin":"IND","Dest":"NYC","Price":100.0},{"Origin":"IND","Dest":"LAX","Price":200.0},{"Origin":"IND","Dest":"SEA","Price":500.0,"PerBagSurcharge":25}]}', "$.Flights[*]", 1) ]%%%%=Field(Row(@r2bj17,1),"NoSuchColumn")=%%]</p>

Show calls that fail in Next (one email each)
<!-- Run each call below in its own Next email: one of them in a combined email
     would stop the whole preview or block the save. -->

<!-- email 1: expect that the email saves, but the preview never shows the new version (no fresh render) -->
%%[ SET @r2bj11 = BuildRowsetFromJSON("", "$.Flights[*]", 1) ]%%%%=RowCount(@r2bj11)=%%

<!-- email 2: expect that the email saves, but the preview fails (HTTP 500) -->
%%[ SET @r2bj13 = BuildRowsetFromJSON(@r2bjunset, "$.Flights[*]", 1) ]%%%%=RowCount(@r2bj13)=%%

<!-- email 3: expect that the email saves, but the preview fails (HTTP 500) -->
%%[ SET @r2bj28 = BuildRowsetFromJSON("{ not json ", "$.Flights[*]", true) SET @r2bj29 = BuildRowsetFromJSON("{ not json ", "$.Flights[*]", "true") SET @r2bj30 = BuildRowsetFromJSON("{ not json ", "$.Flights[*]", "1") ]%%%%=RowCount(@r2bj28)=%%|%%=RowCount(@r2bj29)=%%|%%=RowCount(@r2bj30)=%%

<!-- email 4: expect that the save is rejected (HTTP 400) -->
%%[ SET @r2bj19 = BuildRowsetFromJSON('{"Flights":[{"Origin":"IND","Dest":"NYC","Price":100.0},{"Origin":"IND","Dest":"LAX","Price":200.0},{"Origin":"IND","Dest":"SEA","Price":500.0,"PerBagSurcharge":25}]}', "$.Flights[*]") ]%%X

Differs from official docs

  • Decimals keep their .0. The reference’s flights example shows the prices 100.0, 200.0 and 500.0 as 100, 200 and 500 in the rowset. Next returned 100.0. Card
  • Reading by column number or through Value does not return the data. The reference’s code example reads the columns by number, and a path to plain values is said to give a Value column. In Next, numbered reads on object rows gave empty strings, and on scalar rows Field(row, 1) gave true and Field(row, "Value", 0) gave false; see Field returns the flag. Card
  • The error flag does not match the reference on invalid JSON. The Errors section says 0 raises an exception and 1 gives an empty rowset. In Next, 0 rendered on without failing, 1 on not json gave 0 rows, and an email using true, "true" and "1" on { not json failed to render (the failing spelling was not isolated). Card
  • An empty or unset payload breaks the render. The reference promises an empty rowset for unparseable input with the flag 1. In Next, an unset variable made the preview fail (HTTP 500), and with an empty string the preview never showed a new rendered version. Card

Availability & differences

PlatformAvailable
Marketing Cloud EngagementYes
Marketing Cloud NextYes, from API 67.0

See also