JSON Extender - Missing Values

Started by JTaylor, May 02, 2023, 08:37:16 PM

Previous topic - Next topic

JTaylor

Think of a list of books with item data here....

Is there a straight-forward way to keep moving on when certain keys are missing in JSON?   Maybe there is "synposis" for one book but not another.

Ideally there would be an optional default value parameter for jsValueGet() when a Key is missing.  There are MANY, MANY times when I just want it to return a blank value if it doesn't find a key.   That is almost always the case, actually.

Sorry if I missed something obvious.

Jim

td

Not sure I understand the issue but it may be that you are not using the extender as intended. You should never pass a key-path to jsValueGet when the key does not exist.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

JTaylor

I tend to assume I am always the problem but how do I know if the key exists?    If I have 30 books in a feed.    Some might have 10 fields some might have 40.   I never know book to book which fields will be there.  I can think of some convoluted ways to figure that out but just getting a specified default response, normally blank, would be the ideal solution.


Jim

JTaylor

I know others have this same problem as I recall similar discussions so, for for anyone who is having problems on this front, here is the cleanest solution I know.  This is one of the reasons why I LOVE the substitution option.  Fortunately the error message lends itself to making this possible and easy.  You may will want to add code to handle other errors.

Hopefully a real solution will be forthcoming.  I know it is easy to say that the JSON should always be consistent, and I would love to live in that fantasy world, but in the library world (is this an example of irony?) it is rarely the case where XML and JSON feeds contain fields other than the ones that have values present.

If you don't intend to do anything to solve the problem would you be willing to send me the Extender code in a VS solution?   I can roll one that works without resorting to such tricks like below.   Happy to send the code back.

Thanks.

Jim


Code (winbatch) Select
 

IntControl(73, 2, @TRUE, 1, 0)

publisher = jsValueGet(books,"[books][%x%].[publisher]")

  :WBERRORHANDLER

    If StrTrim(ItemExtract(1,wberrortextstring,":")) == 20108 Then
      %wberroradditionalinfo% = ""
    EndIf
     
    IntControl(73, 2, @TRUE, 1, 0)
 
  Return

kdmoyers

Jim
I wonder if that might be wrapped up into a tidy function?  Not exactly what you wanted, but might tide you over until it arrives?

Code (winbatch) Select

AddExtender("ilcjs44i.dll", 0, "ilcjs64i.dll")

#definefunction ValGet(jobj, key, def)
  x = lasterror() ; eat up prev value
  errormode (@off)
    retval = jsValueGet(jobj,key)
    e = lasterror()
  errormode(@on)
  if e != 0 then retval = def
  return retval
#endfunction

books = $"
{
"deviceSettings" :: [],
"firmware" :: [],
"last" :: 1683282058074
}
$"

books =  jsParse(books)

message("found",      ValGet(books, "last", "whoops") )
message("not found" , ValGet(books, "lost", "whoops") )

exit
The mind is everything; What you think, you become.

JTaylor

Yes.  Would probably be a better way to go.   The option I posted worked well for the project I originally worked it out in but overall what you propose would probably be a better option.  Thanks.

Jim

stanl

A little late to this thread, but I will get a chance to test for missing values. Starting to investigate the Webex API. Used this link to identify room and team id's in order to automate some messaging and group members


https://developer.webex.com/docs/api/v1/rooms/get-room-details


Once Try It is clicked you are assigned a bot and will see a Run button. The return is Json which can be copied to a text file. Some response properties will be missing depending on what is returned for other properties. Once I get a better handle on how to use the initial Json I'll let you know about how missing values were handled.

JTaylor

I will once again ask for an  default value parameter for jsValueGet() when a Key is missing.   I cannot imagine this requires much on the coding side.   I also cannot see how this would not be an EXTREMELY reasonable request.  This situation arises in practically every bit of JSON I encounter so I cannot envision others not wanting this option as well. 

I only wish it had been included when you made the last changes to the extender.  Actually got excited when I saw the last release as I thought that was the change but was then disappointed  :(

Jim

stanl

Quote from: JTaylor on September 20, 2023, 03:21:58 PM
I will once again ask for an  default value parameter for jsValueGet() when a Key is missing.
Jim


I'm late on this. I can understand a value for a key as null, but the key itself missing is something I have never seen in the Json I daily query and transform to either db table or Excel. Json I work with is returned via REST queries. Any keys with no values returned would either indicate NULL or [] => indicating a Json object, possibly an image or sub-property values - but these tend to be consistent across queries so my parsing generally ignores them [luckily no complaints from business]

JTaylor

Most of the feeds I get revolving around book data only include fields/keys that have data.   So it is common for any given records to be some variation of 20-50 fields.  Probably a violation of some standard but that really doesn't matter since I have no control over it.

Jim

kdmoyers

FWIW, this is also my experience: the JSON files I access control a UI thing that has all sorts of options that depend on control types, and, well, they are all different.
The mind is everything; What you think, you become.