How to check if a particular element exists in SuperObject?
Asked Answered
R

2

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?

Resentful answered 25/5, 2014 at 18:2 Comment(10)
Does this work: if obj.AsObject.Exists('AreYouThere?') then?Carolus
@LURD No, there's no such function, that was an example of what I was expecting.Resentful
TSuperObject.AsObject is of type TSuperTableString, which has an Exists() function.Carolus
@LURD Perhaps different version? Because I tried exactly that but there's nothing there. I currently use SuperObject version 1.2Resentful
It is in the trunk for version 1.2, superobject just get it there.Carolus
@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.Resentful
@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 InheritedFieldExistResentful
Just follow my link above and download the zip file.Carolus
@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.Resentful
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.Carolus
C
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;
Carolus answered 25/5, 2014 at 19:44 Comment(2)
It would be so nice if the "latest download" wasn't from 2010 - they need to update that.Resentful
@Jerry, they can't anymore. Google Code dropped download support (which might be the reason of quite huge migrations of several projects from there).Dah
B
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.

Bland answered 25/5, 2014 at 19:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.