Alpha Video Training
Results 1 to 5 of 5

Thread: Xbasic: Checking if a subelement exists

  1. #1
    Member
    Real Name
    Nate Phillips
    Join Date
    Sep 2018
    Posts
    10

    Default Xbasic: Checking if a subelement exists

    Hello!

    This may be a simple question, I'm still learning the ropes of xbasic.

    I'm having an issue checking if a subelement exists or not.

    I make a sql query with xbasic and get back a json with all the data. The data in question can have an array of items associated with it, but it doesn't have to. When it has items there is an items array in the json object, but when it doesn't the items array isn't there. This makes perfect sense, but I can't find a way to check if the items array subelement exists without causing an error. Whenever I try to reference json.items, if it doesn't exist, it throws an error.

    In other languages I can get by this by doing something like

    Code:
      if jsonObject.items .and. jsonObject.items.size() > 0 then
    and if json.items returns null it will just return false for that if statement. It throws and error on the front end here. I guess json.items is not equal to null when it doesn't exist.

    I've also tried if isNull(json.items) and that doesn't work either. I'm guessing it's just having issues because I'm referencing json.items which doesn't exist.

    Is there a good way to check if a subelement exists that doesn't throw an error if it doesn't exist?

    Thanks for your help!
    Last edited by natephillips7; 07-05-2019 at 02:29 PM.

  2. #2
    Member
    Real Name
    Nate Phillips
    Join Date
    Sep 2018
    Posts
    10

    Default Re: Xbasic: Checking if a subelement exists

    I decided to just use an on error goto setup and check for it that way. It does the trick but it's not how I wanted to do it. If anyone has any other ideas for checking it other than using a goto please let me know!

    Thanks!

  3. #3
    Alpha Software Employee Beta Spark's Avatar
    Real Name
    Sarah Mitchell
    Join Date
    Jul 2015
    Posts
    424

    Default Re: Xbasic: Checking if a subelement exists

    Quote Originally Posted by natephillips7 View Post
    Hello!

    This may be a simple question, I'm still learning the ropes of xbasic.

    I'm having an issue checking if a subelement exists or not.

    I make a sql query with xbasic and get back a json with all the data. The data in question can have an array of items associated with it, but it doesn't have to. When it has items there is an items array in the json object, but when it doesn't the items array isn't there. This makes perfect sense, but I can't find a way to check if the items array subelement exists without causing an error. Whenever I try to reference json.items, if it doesn't exist, it throws an error.

    In other languages I can get by this by doing something like

    Code:
      if jsonObject.items .and. jsonObject.items.size() > 0 then
    and if json.items returns null it will just return false for that if statement. It throws and error on the front end here. I guess json.items is not equal to null when it doesn't exist.

    I've also tried if isNull(json.items) and that doesn't work either. I'm guessing it's just having issues because I'm referencing json.items which doesn't exist.

    Is there a good way to check if a subelement exists that doesn't throw an error if it doesn't exist?

    Thanks for your help!
    Use the variable_exists() function. E.g.
    Code:
    if (variable_exists("json.items")) then
     '...
    end if
    Sarah Mitchell

    Looking for answers? Try our documentation.
    If you can't find something, let us know!

  4. #4
    "Certified" Alphaholic
    Real Name
    Gregg Schmidt
    Join Date
    Mar 2001
    Location
    Milwaukee
    Posts
    1,269

    Default Re: Xbasic: Checking if a subelement exists

    Hi Nate,

    I'm guessing there's a little confusion going on here based on what I see.
    I can't tell if jsonObject is meant to be a simple pointer variable, or a pointer variable array (dim jsonObject as p vs dim jsonObject[0] as p).
    The .size() is meant to return the number of items in an array.
    By default, if jsonObject.items.size is valid (eval_valid("jsonObject.items.size") ), jsonObject.items will also be valid because of the sub-element size.
    Here are some basic examples of pointer vs pointer array variables.
    I'm sure we can get what you want, just need everything to be on the same page.

    Gregg

    Code:
    dim jsonObject as p
    jsonObject.items.size=39
    ?eval_valid("jsonObject.items.size")
    = .T.
    
    ?eval_valid("jsonObject.item")
    = .F.
    
    dim examplePointerArray[0] as p
    examplePointerArray[].items.size=39
    examplePointerArray[].items.size=49
    examplePointerArray[].items.size=59
    
    ?examplePointerArray[1].items.size
    = 39
    ?examplePointerArray[3].items.size
    = 59
    
    ?eval_valid("examplePointerArray.items")
    = .F.

  5. #5
    Member
    Real Name
    Nate Phillips
    Join Date
    Sep 2018
    Posts
    10

    Default Re: Xbasic: Checking if a subelement exists

    Awesome thank you for the response!

    I went with variable_exists and it's working great. Thanks again!

Similar Threads

  1. Index Exists?
    By Terrymorgan in forum Windows Desktop Applications
    Replies: 3
    Last Post: 09-01-2018, 09:23 AM
  2. wf.xbasic subelement not found while publishing.
    By CarlMPearson in forum Mobile & Browser Applications
    Replies: 7
    Last Post: 09-27-2016, 05:23 PM
  3. ? File Exists (xBasic)
    By mocnarf in forum Alpha Five Version 10 - Desktop Applications
    Replies: 5
    Last Post: 02-23-2011, 11:12 AM
  4. XBasic method for folder exists?
    By brett s in forum Alpha Five Version 5
    Replies: 3
    Last Post: 06-08-2004, 09:39 PM
  5. Value Exists?
    By Louis Nickerson in forum Alpha Five Version 5
    Replies: 3
    Last Post: 12-05-2002, 10:04 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •