Skip to content

Convert A JSON Value To A String (serialization) ​

Any TJSONValue can be converted (serialized) as a string value using ToJSON method:

JsonString := Object.ToJSON;

pascal
var
  obj: TJSONObject;
begin
  obj := TJSONObject.Create;
  obj['subobject'] := TJSONObject.Create as TJSONValue;
  obj['subobject']['key1'] := TJSONString.Create('value1') as TJSONValue;
  obj['subobject']['key2'] := TJSONNumber.Create(123.45) as TJSONValue;
  obj['foo'] := TJSONString.Create('bar') as TJSONValue;
  LogInfo(obj.ToJSON);
  obj.Free;
end;

Output

{"subobject":{"key1":"value1","key2":123.45},"foo":"bar"}