I have a class for rest request as follows
TRedeemItemsClass = class
private
[JSONName('RedeemCode')]
FRedeemCode: String;
[JSONName('AssetKey')]
FAssetKey:String;
public
property RedeemCode: String read FRedeemCode write FRedeemCode;
property AssetKey:String read FAssetKey write FAssetKey;
function ToJsonString: string;
class function FromJsonString(AJsonString: string): TRedeemItemsClass;
end;
implementation
function TRedeemItemsClass.ToJsonString: string;
begin
result := TJson.ObjectToJsonString(self);
end;
class function TRedeemItemsClass.FromJsonString(AJsonString: string): TRedeemItemsClass;
begin
result := TJson.JsonToObject<TRedeemItemsClass>(AJsonString)
end;
jObj := TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes(reqRedeem.ToJsonString), 0) as TJSONObject;
Using this line of code I get my json request string like
{"RedeemCode":"","AssetKey":"xxxxx"}
as expected.
Redeem request body json string should either one of these ( according to information received from the customer assetkey or redeemcode)
{"RedeemItems":[{"AssetKey":"xxxxx"}]}
or
{"RedeemItems":[{"RedeemCode":"xxxxx"}]}
So in the short term, I want to ignore all fields (including arrays) that is empty or nil.
I'm using Delphi 10 Seattle.
You can use the below method:
function TRedeemItemsClass.ToJsonString: string;
begin
result := TJson.ObjectToJsonString(self, [TJsonOption.joIgnoreEmptyStrings]);
end;
or
function TRedeemItemsClass.ToJsonObject: TJSONObject;
begin
result := TJson.ObjectToJsonObject(self, [TJsonOption.joIgnoreEmptyStrings]);
end;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With