RTC Forums

Subscription => Support => Topic started by: jeff.lott on December 15, 2015, 06:59:58 PM



Title: Return JSON null value
Post by: jeff.lott on December 15, 2015, 06:59:58 PM
Let's say I want to return JSON such as:
{
  "Name" : "Fred Smith",
  "Age" : null
}

NOT: "Age" : "null"
BUT: "Age" : null

procedure xyz(Sender: TRtcConnection);
Var
  Srv:TRtcDataServer absolute Sender;
  Result: TRtcRecord;
Begin
  ..
  Result := TRtcRecord.Create;
  Try
    Result.AsString['Name'] := 'Fred Smith';
    Result.???['Age'] := ???
    Srv.WriteEx(Result.toJSONEx);
  Finally
    FreeAndNil(Result);
  End;
  ...
End

How would I get value of "Age" to be just plain null

Thanks, Jeff


Title: Re: Return JSON null value
Post by: jeff.lott on December 15, 2015, 08:19:05 PM
This seems to work correctly:

  Result.asObject['Age'] := TRtcValue.Create;

Is the the "proper" way to do this, or is there a better way?

Thanks, Jeff


Title: Re: Return JSON null value
Post by: D.Tkalcec (RTC) on December 15, 2015, 11:44:23 PM
That would be one way of doing it.

Another way would be to do it like this:

    Result.asValue['Age'] := Null;

Or (when Result is a TRtcRecord) like this:

    Result['Age'] := Null;

Best Regards,
Danijel Tkalcec


Title: Re: Return JSON null value
Post by: jeff.lott on December 17, 2015, 05:21:47 PM
Ahhh Yes.
I didn't include the Variants unit in my "uses" list.
So I was getting "undeclared identifier NULL" from the compiler when using Result.asValue['Age'] := Null;


Title: Re: Return JSON null value
Post by: D.Tkalcec (RTC) on December 18, 2015, 10:02:17 AM
Yes. Vaiants unit is required for "Null".

Or ... you can do it like this, without using the Variants unit:

 Result.asObject['Age'] := nil;

Best Regards,
Danijel Tkalcec