More Json fun

Started by spl, March 07, 2025, 06:37:30 AM

Previous topic - Next topic

spl

The end of life URL has an api that returns json. The data is verbose and would take some parsing steps to find a bottom line return for a specific product. Code below uses the Json Extender with a variable 'all' that if set to @true will return a list of all products in the database; if @false, all eol dates for a specific product [code picks apple-watch] - seems to be ordered from latest to earliest with 'false' maybe signifying no eol. There are more keys for a product, which the code displays before parsing eol, that may be useful for a nmore accurate return. Anyway, was fun to test and  good exercise for the extender.
AddExtender('ilcjs44i.dll', 0, 'ilcjs64i.dll')
all = @true ;set to @false to test specific product eol

if all
   URL = "https://endoflife.date/api/all.json"
   oHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
   oHTTP.Open("GET", URL, @FALSE)
   oHTTP.Send()
   oHTTP.WaitForResponse()
   strJson = oHTTP.ResponseText
   ;not paired json, so parse out individual products
   strJson = strreplace(strJson,'"','')
   strJson = strreplace(strJson,'[','')
   strJson = strreplace(strJson,']','')
   strJson = strreplace(strJson,',',@LF)
   ;only partial display, probably output to text file or array
   Message('End of Life Products',strJson)
else
   product = 'apple-watch'
   URL = "https://endoflife.date/api/":product:".json"
   oHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
   oHTTP.Open("GET", URL, @FALSE)
   oHTTP.Send()
   oHTTP.WaitForResponse()
   strJson = oHTTP.ResponseText
   Message("json return",strJson) ;just display the return
   Json = jsParse(strJson) 
   Key = '[eol]' ; Key to search for.
  
   Paths = jsKeyPaths(Json , Key)
   Values = ''
   ForEach Path In Paths
      Values := jsValueGet(Json, Path):@lf
   Next
  
   Message(Key:' for ':product, Values)
endif
Exit
Stan - formerly stanl [ex-Pundit]

spl

It seems individual products have keys not in others, although they share some basic keys. The extender serialization is very nice as seen from the display of the code below. Question: is there an easy way to display the return in a ReportView?
AddExtender('ilcjs44i.dll', 0, 'ilcjs64i.dll')
product = 'iphone'
URL = "https://endoflife.date/api/":product:".json"
oHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
oHTTP.Open("GET", URL, @FALSE)
oHTTP.Send()
oHTTP.WaitForResponse()
strJson = oHTTP.ResponseText
Json = jsParse(strJson) 
FormJson =  jsSerialize(Json ,@true, @JsonUTF16)
Message("json return",FormJson) ;just display the return
jsConClose()
Exit
Stan - formerly stanl [ex-Pundit]

td

Can't think of anything easy as you define it, i.e., one-liners. But there is this approach to creating an array to load into a reportview control.
nMax = jsGetInfo(4, Json) - 1
j = 0
For i = 0 to nMax
    container = jsValueGet(Json,'[][':i:']' )
    map = jsConMap(container)
    arr[j] = '' 
    foreach key in map
      arr[j] := key:'.':map[key]:','  
    next
    StrTrimChar(arr[j],',',2)
    j += 1
Next

It's a messy way to create an array for a reportview control. I could refine it a bit by putting each key-value pair into its own column, but it would still be a crude representation.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

spl

Wow. Your code was an impressive display of WB's parsing power given a variety of data type inputs - even if described as 'messy'. Doubt it would be useful to go the length of the EOL return for a product into a reportview, but converting a well-formed json array into a reportview might be valuable for other situations.

This line: container = jsValueGet(Json,'[][':i:']' ) was the most interesting, and non-intuitive to dumb old me.
Stan - formerly stanl [ex-Pundit]