IJsonArrayBuilder.AddElement

Syntax

AddElement(Value: IJsonElement): IJsonArrayBuilder;

AddElement(Value: Prognoz.Platform.Interop.ForeSystem.IJsonElement): Prognoz.Platform.Interop.ForeSystem.IJsonArrayBuilder;

Parameters

Value. JSON structure element.

Description

The AddElement method adds the existing element to JSON array.

Example

Executing the example requires the JSON file: d:\Work\Json\data.json. The file has the following approximate structure:

JSON file

Add links to the ForeSystem (for the Fore.NET example) system assembly.

Sub UserProc;
Var
    Doc: IJsonDocument;
    JSONArray: IJsonArray;
    NewArray: IJsonArrayBuilder;
    NewJSON: IJsonObjectBuilder;
    i, c: Integer;
Begin
    Doc := 
New JsonDocument.Create;
    Doc.ReadFromFile(
"d:\Work\Json\data.json");
    JSONArray := Doc.Root.Query(
"$.product[?(@.price > 150 && @.price < 350)]");
    c := JSONArray.Count;
    
// Create a new JSON structure
    NewArray := New JsonArrayBuilder.Create;
    
For i := 0 To c - 1 Do
        NewArray.AddElement(JSONArray.Item(i));
    
End For;
    NewJSON := 
New JsonObjectBuilder.Create;
    NewJSON.AddElement(
"Products", NewArray.Build);
    
// Save
    Doc.Root := NewJSON.Build;
    Doc.WriteToFile(
"d:\Work\Json\products.json");
End Sub UserProc;

Imports Prognoz.Platform.Interop.ForeSystem;

Sub UserProc();
Var
    Doc: IJsonDocument = 
New JsonDocumentClass();
    JSONArray: IJsonArray;
    NewArray: IJsonArrayBuilder = 
New JsonArrayBuilderClass();
    NewJSON: IJsonObjectBuilder = 
New JsonObjectBuilderClass();
    i, c: Integer;
Begin
    Doc.ReadFromFile(
"d:\Work\Json\data.json");
    JSONArray := Doc.Root.Query(
"$.product[?(@.price > 150 && @.price < 350)]");
    c := JSONArray.Count;
    
// Create a new JSON structure
    For i := 0 To c - 1 Do
        NewArray.AddElement(JSONArray.Item[i]);
    
End For;
    NewJSON.AddElement(
"Products", NewArray.Build());
    
// Save
    Doc.Root := NewJSON.Build();
    Doc.WriteToFile(
"d:\Work\Json\products.json", JsonFormatOptions.jfoDefault);
End Sub;

After executing the example, the JSON structure will be read from the file. This structure will be requested to select elements according to the specified condition. The selected elements will be used to create a new JSON structure, to which the selected elements will be included as an array. The obtained new JSON structure will be saved to a new file.

See also:

IJsonArrayBuilder