In a previous post I wrote how you could use a Notes view as the data source for JSONObjects. In a recent project I needed to minimize the data amount to transfer so I had to exclude the fields that contain no value. Here is the @formula I can up with for the column value:
REM { Patrick Kwinten
Quick way to deliver ‘cached’ JSON objects from Notes documents
};
REM {JSONObject bricks};
jsonOpener := “{“;
jsonSeparator := “,”;
jsonClosure := “}”;
REM {String helper};
src := “\\”:”‘”:@NewLine:”\””;
dst := “\\\\”:”\\'”:”\\n”:”\\\””;
REM {Define which fields to exclude/include in JSON object};
REM {“Idea taken from http://www.eknori.de/2012-06-01/use-transform-to-build-json-and-consume-the-output-in-an-xagent/”};
_exclude:=”$FILE”:”$Fonts”:”form”:”$Revisions”:”ID”:”ModifiedBy”:”$TUA”;
_fld:=@Trim(@ReplaceSubstring(@DocFields;_exclude;@Nothing));
REM {Output for “custom” fields. Empty values are excluded};
_fldOutput := “”;
@For(
n := 1;
n <= @Elements(_fld);
n := n + 1;
@Do(
_fldVal := @Text ( @GetField ( _fld[n] ) );
@If(_fldVal != “” ; _fldOutput := _fldOutput + “\”” + _fld[n] + “\”:\”” + @ReplaceSubstring( _fldVal; Src; Dst ) + “\”” + @If(n != @Elements(_fld); jsonSeparator;””); “”)
)
);
REM {Prepare output};
outputStr := “”;
outputStr := outputStr + jsonOpener;
REM {Output for “default” fields};
tmpUNID:= @Text(@DocumentUniqueID);
outputStr := outputStr + “\”docUNID\”:\”” + tmpUNID + “\”” + jsonSeparator;
outputStr := outputStr + _fldOutput;
REM {Finalize JSONObject};
outputStr := outputStr + jsonClosure;
REM {Return output};
@Return(outputStr)
As a result I have “lesser” JSON Objects in my view: