Skip to main content
3 of 3
edited tags
Adam Spriggs
  • 31.1k
  • 6
  • 44
  • 88

Filtering fields for a JSON output in SSJS

I'm building a SSJS custom API endpoint as a code resource page and have the following code which works fine but I need to limit the fields that are returned from the Data Extension and then placed in the JSON output - it currently returns all fields in the datarows and hence all fields are produced in the JSON array under Data Returned - that's just a dummy name for now.

I can't find a way to do this in the retrieve process, i.e. akin to a SELECT [certain fields only] FROM and am not sure how such a request might be crafted using a loop on the datarows results.

Any ideas / help - and ideally a working example of a loop if possible - would be much appreciated.

var JsonOut;

//initiate DE using it's External Key
var My_DE = DataExtension.Init("EXTERNAL KEY HERE");

//define filter criteria
var filter1 = {
    Property: "Event_Active",
    SimpleOperator: "equals",
    Value: "True"
  };

  var filter2 = {
    Property: "Event_Date",
    SimpleOperator: "greaterThan",
    Value: "1 January 2022"
  };  

  var complexFilter = {
      LeftOperand: filter1,
      LogicalOperator: "AND",
      RightOperand: filter2
    }

//retrieve data
var dataRows = My_DE.Rows.Retrieve(complexFilter);
if (dataRows.length > 0){

//build the return JSON
JsonOut = {
    "Result" : "200 - Success", 
    "Task:" : "Extract",
    "ActiveAfterDate:" : "1 January 2022",
    "Data Returned" : Platform.Function.ParseJSON(Stringify(dataRows))
}

//else conditions etc removed

Write(Platform.Function.Stringify(JsonOut));