Page 1 of 1

Using jsonreader with a file

Posted: Mon Mar 25, 2019 10:10 am
by soa
Elsewhere in this forum there is a suggested way of accessing json data by saving to a temporary text file then using JSONReader.

I can't work out how to access the data within the loop and I can't find any documentation. I can see the JsonReader has TokenAs... properties but I can't work out how to extract the data using these.


Define_Com Class(#PRIM_IOC.FileStream) Name(#FileStream)
Define_Com Class(#PRIM_IOC.StreamReader) Name(#TextReader) Stream(#FileStream)

Define_Com Class(#XPRIM_File) Name(#TempFile)

#TempFile.CreateTemporaryFile

#Request.Response.AsFile Autodelete(True) Path(#TempFile)

#FileStream.Path := #TempFile

Dowhile Cond(#JsonReader.Read)


* DO your stuff here
Endwhile

#PRIM_JSON.Reader

Posted: Mon Mar 25, 2019 5:29 pm
by soa
Okay I've managed to get this PRIM (not XPRIM) stuff going

Define_Com Class(#PRIM_IOC.FileStream) Name(#FileStream)
Define_Com Class(#PRIM_IOC.StreamReader) Name(#TextReader) Stream(#FileStream)
Define_Com Class(#PRIM_JSON.Reader) Name(#JsonReader) Textreader(#TextReader)

You've basically got to handle each token individually to extract the data. See below.

The bottom line is it MUCH faster than the #XPRIM_JSON.Reader. Whereas my orignal version to two minutes to parse out an array 4000 schools. #PRIM_JSON.Reader did it in 4 seconds.

The trade off is that we have to write much more code and for more complex objects would be soon become unmanageable.

#inSchool #inArray := false
Dowhile Cond(#JsonReader.Read)

If Cond(#JsonReader.TokenType = 'BeginArray')
#inArray := true
Continue
Endif

Continue If(*Not #inArray)

If Cond(#JsonReader.TokenType = 'BeginObject')
#inSchool := true
Continue
Endif

If Cond(#JsonReader.TokenType = 'EndObject')
Add_Entry To_List(#xref2)
#DECID #NESAID := *zero
#SCHXRA #SCHLNM := *blank
#inSchool := false
Continue
Endif

If Cond(#inSchool)

If Cond(#JsonReader.TokenType = 'PropertyName')
#propertyname := #JsonReader.TokenText.Trim.UpperCase
* #propertyname := #COM_OWNER.StringValue( #propertyname )
Continue
Endif

If Cond(#JsonReader.TokenType = 'Value')

#wrk256 := #JsonReader.TokenText

#WRK5N0 #wrk6n0 := *zero

Case Of_Field(#propertyname)
When Value_Is(= ACCOUNT)
#SCHXRA := #wrk256
When Value_Is(= ACCOUNTNAME)
#schlnm := #wrk256
When Value_Is(= DECID)
If Cond(#wrk256.IsNumber)
#DECID := #WRK256.AsNumber
Endif
When Value_Is(= NESAID)
If Cond(#wrk256.IsNumber)
#NESAID := #WRK256.AsNumber
Endif
Endcase

Continue
Endif

Endif

Endwhile