Author Topic: JSON Extender - Missing Values  (Read 291 times)

JTaylor

  • Pundit
  • *****
  • Posts: 1928
    • Data & Stuff Inc.
JSON Extender - Missing Values
« on: May 02, 2023, 08:37:16 pm »
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

  • Tech Support
  • *****
  • Posts: 4352
    • WinBatch
Re: JSON Extender - Missing Values
« Reply #1 on: May 02, 2023, 09:18:31 pm »
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

  • Pundit
  • *****
  • Posts: 1928
    • Data & Stuff Inc.
Re: JSON Extender - Missing Values
« Reply #2 on: May 03, 2023, 05:32:20 am »
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

  • Pundit
  • *****
  • Posts: 1928
    • Data & Stuff Inc.
Re: JSON Extender - Missing Values
« Reply #3 on: May 04, 2023, 02:34:47 pm »
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
 

 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

  • Sr. Member
  • ****
  • Posts: 500
Re: JSON Extender - Missing Values
« Reply #4 on: May 05, 2023, 05:21:18 am »
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
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

  • Pundit
  • *****
  • Posts: 1928
    • Data & Stuff Inc.
Re: JSON Extender - Missing Values
« Reply #5 on: May 05, 2023, 05:53:40 am »
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

  • Pundit
  • *****
  • Posts: 1799
Re: JSON Extender - Missing Values
« Reply #6 on: May 11, 2023, 02:42:20 am »
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

  • Pundit
  • *****
  • Posts: 1928
    • Data & Stuff Inc.
Re: JSON Extender - Missing Values
« Reply #7 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.   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