Ewon TechForum
Providing technical solutions to technical requests


JSON parser
simon Offline
eWON Support
#1
Hi Guys,

FYI I have developed a very small JSON parser that works good on simple JSON file structure

Here is the code (composed of two functions)

Code:
Function ParseSimpleJson$($inputJson$, $key$, $CollectionIndex%)
     
     $StartIndex% = 1
     
     FOR $i% = 0 TO 10000
     
       $StartBracketPos% = INSTR $StartIndex%, $inputJson$, '{'
     
       IF $StartBracketPos% = 0 THEN
         $ParseSimpleJson$ = "ELEMENT DOES NOT EXIST"
         RETURN
       ENDIF
       
       IF $CollectionIndex% > 0 THEN
         $CollectionIndex% = $CollectionIndex% - 1
         $StartIndex% = $StartBracketPos% + 1
         //Find next element
       ELSE          
           IF $StartBracketPos% = 0 THEN
             $ParseSimpleJson$ = "JSON FORMAT NOT VALID"
             RETURN
           ELSE        
             $EndBracketPos% = INSTR $StartBracketPos%, $inputJson$, '}'
             $ELEMENTString$ = $inputJson$($StartBracketPos% TO $EndBracketPos%)
             $ParseSimpleJson$ = @ExtractKeyValue$($ELEMENTString$, $key$)
             RETURN
           ENDIF            
       ENDIF
       
     NEXT $i%  
     
EndFn


Function ExtractKeyValue$($ElementString$, $Key$)
 
  $StartPosKey% = INSTR 1,$ElementString$, $key$
  $StartPosKeyColumn% = INSTR $StartPosKey% ,$ElementString$, ":"
  $EndPosKeyColumn% = INSTR $StartPosKeyColumn% ,$ElementString$, ','
 
  $StartPosKeyColumn% = $StartPosKeyColumn% + 1
  $EndPosKeyColumn% = $EndPosKeyColumn% - 1
 
  IF $EndPosKeyColumn% = -1 THEN //Last key
    $EndPosKeyColumn% = LEN $ElementString$ - 1
  ENDIF
 
  $KeyValue$ = $ElementString$($StartPosKeyColumn% TO $EndPosKeyColumn%)
   $KeyValue$ = LTRIM $KeyValue$
   $KeyValue$ = RTRIM $KeyValue$

  IF $KeyValue$(1) = '"' THEN //is a string value -> remove quote
    $EndKeyValue = LEN $KeyValue$ - 1
    $ExtractKeyValue$ = $KeyValue$(2 TO $EndKeyValue)
  ELSE
    $ExtractKeyValue$ = $KeyValue$  
  ENDIF
EndFn

And an example of how to use it :


Code:
ParseFile:
InputStr$ = '[{"opna":"PROXIMUS        ","opid":"  20601","stat":"="},{"opna":"B mobistar      ","opid":"  20610","stat":"-"},{"opna":"BASE            ","opid":"  20620","stat":"-"}]'

indexElement% = 0
Loop:

 A$ = @ParseSimpleJson$(InputStr$,"opna",indexElement%)
 IF A$ = "ELEMENT DOES NOT EXIST" THEN GOTO EndLoop
 PRINT A$
 
 PRINT @ParseSimpleJson$(InputStr$,"opid",indexElement%)
 PRINT @ParseSimpleJson$(InputStr$,"stat",indexElement%)
 indexElement% = indexElement% +1
 
IF indexElement% < 100 THEN GOTO Loop:
EndLoop:
Reply

polishglider Offline
Junior Member
#2
(28-07-2016, 04:09 PM)Simon Wrote: Hi Guys,

FYI I have developed a very small JSON parser that works good on simple JSON file structure

Here is the code (composed of two functions)

Code:
Function ParseSimpleJson$($inputJson$, $key$, $CollectionIndex%)
     
     $StartIndex% = 1
     
     FOR $i% = 0 TO 10000
     
       $StartBracketPos% = INSTR $StartIndex%, $inputJson$, '{'
     
       IF $StartBracketPos% = 0 THEN
         $ParseSimpleJson$ = "ELEMENT DOES NOT EXIST"
         RETURN
       ENDIF
       
       IF $CollectionIndex% > 0 THEN
         $CollectionIndex% = $CollectionIndex% - 1
         $StartIndex% = $StartBracketPos% + 1
         //Find next element
       ELSE          
           IF $StartBracketPos% = 0 THEN
             $ParseSimpleJson$ = "JSON FORMAT NOT VALID"
             RETURN
           ELSE        
             $EndBracketPos% = INSTR $StartBracketPos%, $inputJson$, '}'
             $ELEMENTString$ = $inputJson$($StartBracketPos% TO $EndBracketPos%)
             $ParseSimpleJson$ = @ExtractKeyValue$($ELEMENTString$, $key$)
             RETURN
           ENDIF            
       ENDIF
       
     NEXT $i%  
     
EndFn


Function ExtractKeyValue$($ElementString$, $Key$)
 
  $StartPosKey% = INSTR 1,$ElementString$, $key$
  $StartPosKeyColumn% = INSTR $StartPosKey% ,$ElementString$, ":"
  $EndPosKeyColumn% = INSTR $StartPosKeyColumn% ,$ElementString$, ','
 
  $StartPosKeyColumn% = $StartPosKeyColumn% + 1
  $EndPosKeyColumn% = $EndPosKeyColumn% - 1
 
  IF $EndPosKeyColumn% = -1 THEN //Last key
    $EndPosKeyColumn% = LEN $ElementString$ - 1
  ENDIF
 
  $KeyValue$ = $ElementString$($StartPosKeyColumn% TO $EndPosKeyColumn%)
 
  IF $KeyValue$(1) = '"' THEN //is a string value -> remove quote
    $EndKeyValue = LEN $KeyValue$ - 1
    $ExtractKeyValue$ = $KeyValue$(2 TO $EndKeyValue)
  ELSE
    $ExtractKeyValue$ = $KeyValue$  
  ENDIF
EndFn

And an example of how to use it :


Code:
ParseFile:
InputStr$ = '[{"opna":"PROXIMUS        ","opid":"  20601","stat":"="},{"opna":"B mobistar      ","opid":"  20610","stat":"-"},{"opna":"BASE            ","opid":"  20620","stat":"-"}]'

indexElement% = 0
Loop:

 A$ = @ParseSimpleJson$(InputStr$,"opna",indexElement%)
 IF A$ = "ELEMENT DOES NOT EXIST" THEN GOTO EndLoop
 PRINT A$
 
 PRINT @ParseSimpleJson$(InputStr$,"opid",indexElement%)
 PRINT @ParseSimpleJson$(InputStr$,"stat",indexElement%)
 indexElement% = indexElement% +1
 
IF indexElement% < 100 THEN GOTO Loop:
EndLoop:
Hi Simon

I used this example to extract from a web page. 

I'm a member of Blitzortung Network (World Lightning Detection Network), and I've got access to the entire lightning detection database (because I've got a Strike Detector). With this long range detector, I used an DIY Strike Detector (As3935 Module on Arduino and Ethernet Shield), which communicate with a Flexy in Modbus Ethernet. When a strike occurs, the DIY Module send to the flexy the distance of the strike (up to 50km).Then, the "Parse" script is launch to request the last 10 strikes in my area, save data on txt file. This file contain the GPS positions, and the timestamp of each strike. The basic script extract all the data, read the timestamp, and compare it with realtime hour (and keep only the last hour strikes), and send them to an array on a Allen Bradley Flexlogix PLC. The PLC calculate the distance of every strike, and send the minus one to the ewon, which send me an SMS. All positions are displayed on a map, thanks to markers on a PCVue Application. Every 15 minutes, the script connect to the lightning servers to check if no strike occurs near.
Reply



Possibly Related Threads…
  CSV Parser function Started by simon
2 Replies - 1.634 Views
01-05-2020, 11:33 AM
Last Post: simon
08-04-2020, 05:23 PM
Last Post: simon



Users browsing this thread:
2 Guest(s)



Theme © Ewon 2019 - Forum software by © MyBB - Cookie policy