RTL.JSONWriter

From RAD Studio Code Examples
Jump to: navigation, search

Description

This code snippet shows how to write JSON content using the System.JSON.Writers framework.

Code

The following JSON object contains information about purchase records from a website.

{"Transaction":[ 
  {"id":662713, "firstName":"John", "lastName":"Doe", "price": 2.1, "parent_id": null, validated:true},
  {"id":662714, "firstName":"Anna", "lastName":"Smith", "price": 4.5, "parent_id": null, validated: false}, 
  {"id":662715, "firstName":"Peter", "lastName":"Jones", "price": 3.6, "parent_id": null, validated: true} 
  ]}

This sample illustrates how to write the JSON object provided. The JSON object created is printed to the console.

The Formatting property of the JSON writer is set to Indented to generate formatted easy-to-read JSON content, applying indentation to same name/value pairs level.

uses
  System.SysUtils,
  System.Classes,
  System.JSON.Types,
  System.JSON.Writers;

var
  Writer: TJsonTextWriter;
  StringWriter: TStringWriter;

begin
  StringWriter := TStringWriter.Create();
  Writer := TJsonTextWriter.Create(StringWriter);
  Writer.Formatting := TJsonFormatting.Indented;

  Writer.WriteStartObject;
  Writer.WritePropertyName('Transaction');
  Writer.WriteStartArray;
  Writer.WriteStartObject;
  Writer.WritePropertyName('id');
  Writer.WriteValue(662713);
  Writer.WritePropertyName('firstName');
  Writer.WriteValue('John');
  Writer.WritePropertyName('lastName');
  Writer.WriteValue('Doe');
  Writer.WritePropertyName('price');
  Writer.WriteValue(2.1);
  Writer.WritePropertyName('parent_id');
  Writer.WriteNull;
  Writer.WritePropertyName('validated');
  Writer.WriteValue(-1);
  Writer.WriteEndObject;
  Writer.WriteStartObject;
  Writer.WritePropertyName('id');
  Writer.WriteValue(662714);
  Writer.WritePropertyName('firstName');
  Writer.WriteValue('Anna');
  Writer.WritePropertyName('lastName');
  Writer.WriteValue('Smith');
  Writer.WritePropertyName('price');
  Writer.WriteValue(4.5);
  Writer.WritePropertyName('parent_id');
  Writer.WriteNull;
  Writer.WritePropertyName('validated');
  Writer.WriteValue(-1);
  Writer.WriteEndObject;
  Writer.WriteStartObject;
  Writer.WritePropertyName('id');
  Writer.WriteValue(662715);
  Writer.WritePropertyName('firstName');
  Writer.WriteValue('Peter');
  Writer.WritePropertyName('lastName');
  Writer.WriteValue('Jones');
  Writer.WritePropertyName('price');
  Writer.WriteValue(3.6);
  Writer.WritePropertyName('parent_id');
  Writer.WriteNull;
  Writer.WritePropertyName('validated');
  Writer.WriteValue(-1);
  Writer.WriteEndObject;
  Writer.WriteEndArray;
  Writer.WriteEndObject;

  WriteLn(StringWriter.ToString);

This is the JSON content from the console:

{
  "Transaction": [
    {
      "id": 662713,
      "firstName": "John",
      "lastName": "Doe",
      "price": 2.1,
      "parent_id": null,
      "validated": -1
    },
    {
      "id": 662714,
      "firstName": "Anna",
      "lastName": "Smith",
      "price": 4.5,
      "parent_id": null,
      "validated": -1
    },
    {
      "id": 662715,
      "firstName": "Peter",
      "lastName": "Jones",
      "price": 3.6,
      "parent_id": null,
      "validated": -1
    }
  ]
}

Uses

See Also