0

There's code a minimum reproducible example in my previous question. Please let me know if you want it copied here.

Basically, I noticed that this code:

if Assigned(job['Employer.Name.Normalized']) then
   Memo1.Lines.Add('Employer name = ' + job['Employer.Name.Normalized'].AsString);

was adding an empty string to the memo.

Hardly surprising, as the JSON looks like this:

                    "Id": "POS-10",
                    "Employer": {
                        "Location": {
                            "CountryCode": "UK",
                            "Municipality": "Bradford"
                        }
                    },
                    "IsSelfEmployed": false,
                    "IsCurrent": false,
                    ... etc

So, why does Assigned(job['Employer.Name.Normalized']) evaluate to true?

Note that Assigned(job.O['Employer'].O['Name'].O['Normalized']) gives the same result, but, AFIAK, it's just syntactic sugar as how it is written.

So, I tried random keys: Assigned(job['StackOverlow]) evaluates to true, as does Assigned(job['Why is this not false???'])

What am I doing wrongly? And how to I detect if a key actually exists in the ISuperObject which was obtained from my JSON?

3
  • You are using a derived work. Did you try with the original which can be found at github.com/hgourvest/superobject.
    – fpiette
    Feb 4 at 13:31
  • The GitHub page says that it is no longer maintained, which is why I chose X-SuperObject. But, I can't believe it is so flawed Much more likely that I am doing something wrongly. I'd no answers, I will try your suggestion Feb 4 at 15:20
  • "Please let me know if you want [code from other question] copied here" - yes, please. StackOverflow questions are meant to be self-contained. They can certainly reference each other, but they should contain all relevant details in themselves. Feb 4 at 17:37

1 Answer 1

4

This is working as designed.

Looking at XSuperObject's source code, it turns out that when you reference a member object or array, and that member doesn't exist, XSuperObject creates a new member. This lets users easily create new JSON documents.

To search for a member without creating it, you can use the Contains() method instead, eg:

if job.Contains('Employer') then
begin
  emp := job.O['Employer'];
  if emp.Contains('Name') then
  begin
    nam := emp.O['Name'];
    if nam.Contains('Normalized') then
      Memo1.Lines.Add('Employer name = ' + nam.S['Normalized']);
  end;
end;
2
  • Thanks, as always, Remy. It's interesting to note that for nested JSOM, I can't use job.Contains('Employer.Location'), but that job.O['Employer'].Contains('Location') is fine. BTW, do you know this form reading the code? Isn't there any decent documentation? Feb 5 at 11:07
  • 1
    "BTW, do you know this form reading the code?" - yes. "Isn't there any decent documentation?" - I don't know, ask the author Feb 6 at 15:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.