1. Joe Hess
  2. PowerBuilder
  3. Friday, 31 August 2018 18:04 PM UTC

I'm consuming a web service which returns values in JSON format. The models are predefined, but some values are optional. The optional values may not be sent at all. Is there an easy way to check if a key exists prior to calling a getItem method?

Right now I'm just loading a structure using the getItem methods.

Thank you.

Accepted Answer
Armeen Mazda @Appeon Accepted Answer Pending Moderation
  1. Thursday, 6 September 2018 07:34 AM UTC
  2. PowerBuilder
  3. # Permalink

We plan on making an enhancement in PB 2018 to address this.  PB 2018 is full of many features so there is small chance this will slip so check the roadmap page around November 2018 to get a sense of the final feature set of PB 2018: https://www.appeon.com/developers/roadmap

 

Comment
There are no comments made yet.
Chris Pollach @Appeon Accepted Answer Pending Moderation
  1. Tuesday, 4 September 2018 18:26 PM UTC
  2. PowerBuilder
  3. # 1

Hi Joe;

    Here is a good simple example:   http://services.groupkt.com/state/get/USA/all

    You will notice that around row 50 in the returned result set that the data element largest_city is no longer returned on some rows. This causes the PB App to crash on the statement ...

ls_data    =    io_parser.GetItemString ( ll_child, "largest_city" )

    No matter how you code - even with a TRY..CATCH - once you issue the GetItem() for an optional data element you can no longer recover!

Regards ... Chris

Comment
  1. Joe Hess
  2. Tuesday, 4 September 2018 20:42 PM UTC
Hey, Chris.



That is still just a missing key from an array element. I can step through the children and use GetChildKey to see if 'largest_city' exists prior to calling GetItemString.



Joe
  1. Helpful
  1. Chris Pollach @Appeon
  2. Tuesday, 4 September 2018 22:03 PM UTC
Correct. However, if you do not know which element is optionally going to be random - then you have to use the GetChildKey() method on every element and for every row. Very unproductive vs just having PB return a NULL.
  1. Helpful
There are no comments made yet.
Roland Smith Accepted Answer Pending Moderation
  1. Saturday, 1 September 2018 02:49 AM UTC
  2. PowerBuilder
  3. # 2

If the GetItem function returned a null when not found, you could test with IsNull.

Comment
  1. Chris Pollach @Appeon
  2. Saturday, 1 September 2018 03:11 AM UTC
That's the problem, it doesn't and errors out the PB App instead. Currently, there is no way to catch the GetItem failure. Returning a null would be one fix not yet implemented in R3
  1. Helpful
There are no comments made yet.
Chris Pollach @Appeon Accepted Answer Pending Moderation
  1. Saturday, 1 September 2018 02:36 AM UTC
  2. PowerBuilder
  3. # 3

Hi Joe;

   I have reported this as an issue in PB2017R2 as the GetItem will error if the optional field is not there. The JSON object does not have Error Events like the DW/DS controls do to handle this situation. Worst of all, a TRY..CATCH does not trap the GetItem failure either. I  believe that this is still an issue in R3 as well.

Regards ... Chris

Comment
There are no comments made yet.
Joe Hess Accepted Answer Pending Moderation
  1. Friday, 31 August 2018 19:02 PM UTC
  2. PowerBuilder
  3. # 4

For now, I created a keyExists method that scans the children, returning True if a child with the name exists.

Comment
  1. Chris Pollach @Appeon
  2. Saturday, 1 September 2018 02:39 AM UTC
That will work for keys, but not other missing optional fields.

The only work around would be to scan for all fields on each row. While this is possible, its extremely script and processing intensive.
  1. Helpful
  1. Joe Hess
  2. Tuesday, 4 September 2018 17:49 PM UTC
Chris, can you give me an example of what an "other missing optional field" might be? I thought JSON consisted of keys and values where a value could also be a JSON object or array.



Thanks
  1. Helpful
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.