JSON Help: Redux

Started by stanl, September 26, 2020, 02:15:44 PM

Previous topic - Next topic

stanl

The original thread is now a little confusing. To sum up, after making a request to https://api.weather.gov/alerts/ for an active state, Json is returned. Kirby suggested RexEx code to parse out essential sections of the Json. But, Houston, we might have a problem:


The script below [using WB CLR] outputs the Json to a file in C:\temp then runs Kirby's [slightly modified] code to show results. As the url updates at regular intervals, the data might not be the same but: 




"headline": "Flood Warning issued September 26 at 2:21PM EDT until September 29 at 3:42AM EDT by NWS Raleigh NC",
                "description": "...The Flood Warning is extended for the following rivers in North\nCarolina...\n\nLittle River At Manchester affecting Cumberland County.\n\nFor the Little River...including Manchester...Minor flooding is\nforecast.\n\nThe Flood Warning continues for\nthe Little River At Manchester.\n* Until late Monday night.\n* At 2:00 PM EDT Saturday the stage was 18.2 feet.\n* Flood stage is 18.0 feet.\n* Minor flooding is occurring and minor flooding is forecast.\n* Recent Activity...The maximum river stage in the 24 hours ending\nat 2:00 PM EDT Saturday was 18.2 feet.\n* Forecast...The river is expected to rise to a crest of 20.8 feet\ntomorrow morning. It will then fall below flood stage Monday\nafternoon.\n* Impact...At 18.0 feet, Flood stage. Minor flood problems begin in\nFort Bragg near the water treatment plant.\n* Flood History...This crest compares to a previous crest of 21.9\nfeet on 01/31/2020.",
                "instruction": "Turn around, don't drown when encountering flooded roads. Most flood\ndeaths occur in vehicles.\n\nAdditional information is available at www.weather.gov/rah/rivers.\n\nThe next statement will be issued late tonight by 230 AM EDT.",
                "response": "Avoid",
                "parameters": {
                    "NWSheadline": [
                        "The Flood Warning is now in effect until early Tuesday morning"
                    ],
                    "VTEC": [
                        "/O.EXT.KRAH.FL.W.0049.000000T0000Z-200929T0742Z/"
                    ],
                    "EAS-ORG": [
                        "WXR"
                    ],
                    "PIL": [
                        "RAHFLSRAH"
                    ],
                    "BLOCKCHANNEL": [
                        "CMAS",



Is what should be found. I'm going to mess with ,and probably mess up, the Regex and maybe this is best handled adding NewtonSoft, or sliding in some Powershell code.  Anyway, here is a workable script:


Code (WINBATCH) Select


;Winbatch Json Parse with Regex
IntControl(73,1,0,0,0)
request = "https://api.weather.gov/alerts/active?area=NC"
cFile = "C:\temp\NC.json"
If FileExist(cFile) Then FileDelete(cFile)
ObjectClrOption("useany", "System")
ObjectClrOption("useany", "System.Net.Http")               




oClient = ObjectClrNew('System.Net.Http.HttpClient')
oTask = ObjectClrNew('System.Threading.Tasks.Task')
oResponse = ObjectClrNew('System.Net.Http.HttpResponseMessage')
oClient.DefaultRequestHeaders.Add("User-Agent","Other")


Response = oClient.GetAsync(request).Result
n=0
While n<=10
   If Response.IsSuccessStatusCode Then Break
   TimeDelay(1)
   n=n+1
   If n>9
      Pause("Giving Up","Unable To Create Request")
      goto theend
   Endif
EndWhile
TaskStr = Response.Content.ReadAsStringAsync()
html=TaskStr.Result
FilePut(cFile,html)


:theend
oTask = 0
oClient = 0
If FileExist(cFile)
   Display(2,"Json Output Saved",cFile)
   ;now Kirby's code
   text = Fileget(cFile)
   regexpr = '"headline": "([^"]+)",\r\n *"description": "(.+?)"'


   REX = "System.Text.RegularExpressions"
   ObjectClrOption('useany', REX)
   R = ObjectClrNew(REX:'.RegexOptions')
   R = R.IgnoreCase | R.Multiline | R.Singleline
   ReOx = ObjectClrType(REX:'.RegexOptions',R)
   RE = ObjectClrNew(REX:'.Regex', regexpr, ReOx)


   matches = RE.Matches(text)
   If matches.Count > 0
      foreach match in matches
         groups = match.Groups
         t1 = groups.Item(1).Captures.Item(0).Value
         t2 = groups.Item(2).Captures.Item(0).Value
         message(t1,t2)     
      next
   Else
      Display(2,"File Created: ":cFile,"But No Alerts To Display")
   Endif
Else
   Display(2,"Web Request Failed",cFile:" not Created")
Endif




Exit


:WBERRORHANDLER
oTask = 0
oClient = 0
wberroradditionalinfo = wberrorarray[6]
lasterr = wberrorarray[0]
handlerline = wberrorarray[1]
textstring = wberrorarray[5]
linenumber = wberrorarray[8]
errmsg = "Error: ":lasterr:@LF:textstring:@LF:"Line (":linenumber:")":@LF:wberroradditionalinfo
Terminate(@TRUE,"Error Encountered",errmsg)

kdmoyers

I think the RegEx fails because the file is LF terminated, not CRLF terminated.
We can add a question mark after the \r to make the CR optional:
Code (winbatch) Select
  regexpr = '"headline": "([^"]+)",\r?\n *"description": "(.+?)"'
Now it finds three matches.
-Kirby
The mind is everything; What you think, you become.

stanl

Quote from: kdmoyers on September 28, 2020, 04:56:50 AM
I think the RegEx fails because the file is LF terminated, not CRLF terminated.
We can add a question mark after the \r to make the CR optional:
Code (winbatch) Select
  regexpr = '"headline": "([^"]+)",\r?\n *"description": "(.+?)"'
Now it finds three matches.
-Kirby


Nice. I ask the question and the answer is a ?.  Hope you agree it is some interesting Json. For a nicer touch


Code (WINBATCH) Select


         t1 = StrReplace(groups.Item(1).Captures.Item(0).Value,"\n",@LF)
         t2 = StrReplace(groups.Item(2).Captures.Item(0).Value,"\n",@LF)