AddElement(Value: IJsonElement): IJsonArrayBuilder;
Value. JSON structure element.
The AddElement method adds the existing element to JSON array.
Executing the example requires the JSON file: d:\Work\Json\data.json. The file has the following approximate structure:
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.RootElement.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.RootElement := NewJSON.Build;
Doc.WriteToFile("d:\Work\Json\products.json");
End Sub UserProc;
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: