#1 2025-01-30 08:28:07

anouri
Member
Registered: 2024-02-11
Posts: 76

How can I remove fields with null value from result Json?

I have class like this:

type
  TMyCalss = class
  private
    fid: integer;
    fqty:TNullableInteger;
    fdescr:TNullableUtf8;
  published
    property id: integer read fid write fid;
    property qty: TNullableInteger read fqty write fqty;
    property descr: TNullableUtf8 read fdescr write fdescr;
end;

json :=  ObjectToJson(MyCalss ,[]);

is there an option create json without null values?

[{"id":1,"qty":null,"fdesc":"descr"}]   =>   [{"id":1,"fdesc":"descr"}]

It is nice TTextWriterWriteObjectOption include woReomveNullValues or something like that

Last edited by anouri (2025-01-30 08:49:58)

Offline

#2 2025-01-30 08:58:51

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,845
Website

Re: How can I remove fields with null value from result Json?

Did you try with woDontStoreVoid ?

Offline

#3 2025-01-30 09:10:37

anouri
Member
Registered: 2024-02-11
Posts: 76

Re: How can I remove fields with null value from result Json?

Yes it works.
But when the field is of arrayof  class type and is not assigned, it will also delete it.

type
  TMyCalss = class
  private
    fid: integer;
    fqty:TNullableInteger;
    FOtherClass:TArrayOfOtherClass;
  published
    property id: integer read fid write fid;
    property qty: TNullableInteger read fqty write fqty;
    property OtherClass: TArrayOfOtherClass read FOtherClass write FOtherClass;
end;

In many cases api report this as error and expect [] for nil array.

Last edited by anouri (2025-01-30 09:21:21)

Offline

#4 2025-01-30 09:37:28

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,845
Website

Re: How can I remove fields with null value from result Json?

This begins to be a lot of specific cases.
We can't change a behavior which was defined since years, and adding new options would always be more confusing.

Offline

#5 2025-01-30 10:12:52

anouri
Member
Registered: 2024-02-11
Posts: 76

Re: How can I remove fields with null value from result Json?

I find a way:

var
  MyCalss: TMyCalss;
  json: string;
  MyArray: TMyArray;
begin
  SetLength(MyArray, 1);
  MyArray[0] := TClass.Create;

  MyCalss := TMyCalss.Create;
  MyCalss.ID := 1;
  MyCalss.ArrayClass := MyArray;

  json :=  ObjectToJson(MyCalss,[woDontStoreVoid]);
  json := StringReplace(json,'[{}]','[]',[rfReplaceAll]);

result => {"ID":1,"ArrayClass":[{}]}
then StringReplace replace [{}] with [].
I hope it is safe way!

Thank you for great framewotk

Last edited by anouri (2025-01-30 10:14:44)

Offline

#6 2025-01-30 10:26:05

anouri
Member
Registered: 2024-02-11
Posts: 76

Re: How can I remove fields with null value from result Json?

I confused with woDontStoreVoid because of documentation in mormot.core.text:
  // - woDontStoreVoid will avoid serializing numeric properties equal to 0 and
  // string properties equal to '' (replace both deprecated woDontStore0 and

behaviour is different:
when numeric field has 0 value or string has '' it serialize it to {"num":0,"str":""}. but when it is null doesn't serialize it.
I think documentation need to be correct?

Last edited by anouri (2025-01-30 11:11:15)

Offline

Board footer

Powered by FluxBB