I can't delete a JSON Object Pair given a name.

In WebLin.JSON.pas unit, you have the following method:

function TJSONObject.RemovePair(const PairName: string): TJSONPair;
var
  o: TJSObject;
begin
  Result := nil;
  if Assigned(fjo) then
  begin
    o := fjo;
    asm
      delete o[PairName];
    end;
  end;
end;

Why do you test if fjo is assigned if it is not an object, Is a "PairName :string" what I want to remove?

FJO is the internal JSObject handled by TJSONObject.
"Your pair" should be there.

  TJSONObject = class(TJSONValue)
  private
    fjo: TJSObject;

Is not there, because I've created the JSON like this.

JSONObject := await(TJSONObject, TDataMng.DataSetToJSON(cdsCallUps));
   try
      JSONObject.RemovePair('DT_CALL_UP');
      JSONObject.AddPair('DT_CALL_UP', TTypeConv.DateTimeToJSON(cdsCallUpsDT_CALL_UP.AsDateTime));
      //await(TModalResult, MessageDlgAsync(TTypeConv.DateTimeToJSON(cdsCallUpsDT_CALL_UP.AsDateTime), mtConfirmation, [mbOk]));
      //await(TModalResult, MessageDlgAsync(JSONObject.ToString, mtConfirmation, [mbOk]));
      Request.PostData := JSONObject.ToString;

      {"CD_TEAM":"asdfasdf",
      "DT_CALL_UP":"04/23/2024",
      "DS_TEAM":"ASDFASDFff",
      "MATCH":"MATCH",
      "MEETING_POINT":"MEETING_POINT",
      "LOCATION":"LOCATION",
      "TRANSPORTATION":"TRANSPORTATION",
      "TRAVEL_BY":"TRAVEL_BY",
      "UNIFORM":"UNIFORM",
      "NOTES":"",
      "DT_CALL_UP":"04/23/2024 14:30"}

   finally
      JSONObject.Free;
   end;

You can see the resulting values in the comment.

Seems like it should use FMembers list when fjo is not assigned, but it doesn't.

Was indeed an oversight. We will have this fixed for the next update.

In the same sense, can you make FMembers public or create another form of traversing all pairs of a TObject?

I'm trying to create a method that copies all the members in a new TJSONObject except the one I want to substitute, but I can't traverse the complete list of members.

Do you know some tip to traverse all the pairs of a TJSONObject by his name?

You can use

  TJSONObject = class(TJSONValue)
  published
    property Pairs[const Index: Integer]: TJSONPair read GetPair;
    property Count: Integer read GetCount;
  end;

Ok. But How can I get the Name of the TJSONPair?

A pair, in origin, is a Name and its value.
But the TJSONPair don't have a property called "Name".

Like this?

class function TDataMng.RemoveAPair(const JSONObject :TJSONObject; PairName :string): TJSONObject;
var Pair :TJSONPair;
    i    :Integer;
begin
   Result := TJSONObject.Create;
   for i := 0 to JSONObject.Count - 1 do begin
      Pair := JSONObject.Pairs[i];
      if Pair.JsonString.Value <> PairName then begin
         Result.AddPair(Pair.JsonString.Value, Pair.JsonValue.Value);
      end;
   end;
end;

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

When I use this code:

var
  jo: TJSONObject;
  i: integer;
  jp: TJSONPair;
begin
  jo := TJSONOBject.Create;
  jo.AddPair(TJSONPair.Create('Name','Joe'));
  jo.AddPair(TJSONPair.Create('City','Paris'));


  for i := 0 to jo.Count - 1 do
  begin
    jp := jo.Pairs[i];
    console.log(jp.JsonString.Value, jp.JsonValue.Value);
  end;

  jo.Free;
end;

it shows the name & value of each pair added