7

I widely use the SuperObject JSON library. I need to be able to check if a particular element exists in an object or not. I can check the value of an element, for example an integer that doesn't exist returns 0. However, 0 is one of the possible values if it does exist - so I can't depend on observing 0 for the element's existence. I checked the ISuperObject for methods that can do this (for example I'd expect something like ISuperObject.Exists(const S: String): Boolean;), but see nothing like this.

How can I check if a particular element exists in the JSON object or not?

Jerry Dodge
  • 26,858
  • 31
  • 155
  • 327
  • Does this work: `if obj.AsObject.Exists('AreYouThere?') then`? – LU RD May 25 '14 at 18:57
  • @LURD No, there's no such function, that was an example of what I was expecting. – Jerry Dodge May 25 '14 at 19:04
  • 1
    `TSuperObject.AsObject` is of type `TSuperTableString`, which has an `Exists()` function. – LU RD May 25 '14 at 19:09
  • @LURD Perhaps different version? Because I tried exactly that but there's nothing there. I currently use SuperObject version 1.2 – Jerry Dodge May 25 '14 at 19:10
  • It is in the trunk for version 1.2, [superobject](https://code.google.com/p/superobject/source/browse/superobject.pas) just get it there. – LU RD May 25 '14 at 19:30
  • @LURD Hmm, 1.2.4 version history still ends at just 1.2, now I have to re-compile my library and all applications that use it :-| Post an answer with this and I'll accept that one. – Jerry Dodge May 25 '14 at 19:32
  • @LURD I just did a search on the latest version's source and there's still no such function, are you sure your copy wasn't customized? The only thing I see with the word "exist" is `InheritedFieldExist` – Jerry Dodge May 25 '14 at 19:34
  • Just follow my link above and download the zip file. – LU RD May 25 '14 at 19:38
  • @LURD Okay I'm confused, I downloaded the latest ZIP file for 1.2.4 and didn't find this. The link you provided does have this. And a whole lot more units than the main download did. – Jerry Dodge May 25 '14 at 19:39
  • 2
    This is often the case, so I have a habit of always checking out the latest update from the trunk. You can see that version 1.2.4 is from 2010, while the latest update was made this year. – LU RD May 25 '14 at 19:51

2 Answers2

8

The latest update of SuperObject contains an Exists() function.

var
  obj : ISuperObject;
begin
  obj := TSuperObject.ParseFile('..\..\SAMPLE.JSON',FALSE);
  if not obj.AsObject.Exists('FindMe') then begin
    WriteLn('Not found');
  end;
end;

If you should use the dwsJSON parser instead, there is a similar function to use:

if json['DoesNotExists'].ElementCount = 0 then begin
  WriteLn('Not found');
end;
LU RD
  • 34,438
  • 5
  • 88
  • 296
  • It would be so nice if the "latest download" wasn't from 2010 - they need to update that. – Jerry Dodge May 25 '14 at 20:03
  • 1
    @Jerry, they can't anymore. Google Code dropped download support (which might be the reason of quite huge migrations of several projects from there). – TLama May 26 '14 at 03:35
5

You can check if certain field exists like this:

function FieldExists(const ASuperObject: ISuperObject; const AField: String): Boolean;
var
  o: ISuperObject;
begin
  o := ASuperObject.O[AField];
  result := Assigned(o);
end;

Basically, json_superobject.O[field_name] should return pointer to ISuperObject if field_name exists. Otherwise, it returns nil.

rsrx
  • 1,433
  • 1
  • 13
  • 25