All Things WinBatch > WinBatch

JSON Extender - Missing Values

(1/3) > >>

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.


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.

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.


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.



--- Code: Winbatch --- 

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

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


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

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()
  if e != 0 then retval = def
  return retval

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

books =  jsParse(books)

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



[0] Message Index

[#] Next page

Go to full version