Anybody written a JSON Parser?

More advanced topics discussed.

Moderators: Susan Smith, admin, Gabriel

bluesfannoz
Posts: 291
Joined: Fri Jun 19, 2009 9:01 am
Location: Lawrence, Kansas
Contact:

Anybody written a JSON Parser?

Post by bluesfannoz »

Subject says it all. Checking to see if anyone has written a JSON parser in BR and would be willing to share it, before I embark on it?
Steve Koger
Computer Specialist
SEKESC-MACS Division
GomezL
Posts: 258
Joined: Wed Apr 29, 2009 5:51 am
Contact:

Re: Anybody written a JSON Parser?

Post by GomezL »

This is 100% not a comprehensive JSON parser, but I used it for a Known JSON source:

Code: Select all

01020 PARSE_LINE: ! 
01030     LET _Pos1=Pos(Json$,"{")
01040     LET _Pos2=Pos(Json$,"}")
01050     DO While _Pos1>0 And _Pos2>0
01060       LET Json_Line$=Trim$(Json$(_Pos1+1:_Pos2-1))
01070       LET Json$(_Pos1:_Pos2)=""
01080       LET _Pos1=Pos(Json$,"{")
01090       LET _Pos2=Pos(Json$,"}")
01100       LET Str2mat(Json_Line$,Mat Json_Csv$,",","Q:trim")
01110       LET Json_Elements=Udim(Json_Csv$)
01120       MAT Json_Key$(Json_Elements)=("")
01130       MAT Json_Data$(Json_Elements)=("")
01140       FOR _Json_Element=1 TO Json_Elements
01150         LET Str2mat(Json_Csv$(_Json_Element),Mat Json_Pair$,":","Q:trim")
01160         LET Json_Key$(_Json_Element)=Uprc$(Json_Pair$(1))
01170         LET Json_Data$(_Json_Element)=Uprc$(Json_Pair$(2))
01180       NEXT _Json_Element

01250     LOOP  ! 
bluesfannoz
Posts: 291
Joined: Fri Jun 19, 2009 9:01 am
Location: Lawrence, Kansas
Contact:

Re: Anybody written a JSON Parser?

Post by bluesfannoz »

Perfect! Was quickly able to adapt it for my needs!

Thanks Luis!
Steve Koger
Computer Specialist
SEKESC-MACS Division
gordon
Posts: 358
Joined: Fri Apr 24, 2009 6:02 pm

Re: Anybody written a JSON Parser?

Post by gordon »

Not sure how this was missed. I thought I mentioned it at the conferences.

The BR Web Server has a built-in JSON parser with about a dozen library functions.
This can be used as a library whether or not you use the web server to serve web pages.

You can store data between calls and manipulate JSON in its natural state.

You can also move JSON objects to BR arrays and create JSON from BR arrays
one layer at a time.

I have attached the manual that describes those functions.
Attachments
05 DATA STORE LIBRARY.doc
(68 KiB) Downloaded 664 times
bluesfannoz
Posts: 291
Joined: Fri Jun 19, 2009 9:01 am
Location: Lawrence, Kansas
Contact:

Re: Anybody written a JSON Parser?

Post by bluesfannoz »

Great! Next obvious question.

I don't have the webserver. Do I have to get it? If so where?
Steve Koger
Computer Specialist
SEKESC-MACS Division
gordon
Posts: 358
Joined: Fri Apr 24, 2009 6:02 pm

Re: Anybody written a JSON Parser?

Post by gordon »

The current edition of the web server is attached.
Attachments
web_server.bro
(47.17 KiB) Downloaded 668 times
gordon
Posts: 358
Joined: Fri Apr 24, 2009 6:02 pm

Re: Anybody written a JSON Parser?

Post by gordon »

The BR JSON library is extensive and well documented.

And it is free. It is the library built into the BR Web Server. This library can be used independently of using the web server to serve web pages. In other words the web server program is also a library that doesn't need to be running on its own to be called by your programs. This includes the Data Store which is a way of storing session related data. Both of these capabilities are described in the attached document. This document and the latest web_server program are available at ftp://ftp.brulescorp.com/Dll_Distr/web_server/.

Additionally, the BR JSON parser does much more than parse JSON. It lets you CHANGE EXISTING JSON directly without having to parse it. Therefore if you receive JSON from an application and need to update it, you can do so without having to decompose the JSON layer by layer.

The Data Store lets you store JSON and/or any character values and retrieve them by name. And you can update (change) stored JSON directly without having to retrieve it.
05 DATA STORE LIBRARY.doc
(68 KiB) Downloaded 674 times
bluesfannoz
Posts: 291
Joined: Fri Jun 19, 2009 9:01 am
Location: Lawrence, Kansas
Contact:

Re: Anybody written a JSON Parser?

Post by bluesfannoz »

Where you have some good working examples in the Data Store Library Documentation.

This particular function is missing a working example.

Code: Select all

FNParse_Json( string-expression, value-type, member-name, mat keys$, mat values$)
Does anyone have working example of this function from web_server.bro?
Steve Koger
Computer Specialist
SEKESC-MACS Division
Mikhail
Posts: 87
Joined: Tue Jul 07, 2009 10:26 am
Location: Ukraine

Re: Anybody written a JSON Parser?

Post by Mikhail »

00001 dim Keys$(0)*255,Values$(0)*255
00002 dim Json$*30000 ! Max Allowed By Fnparse_Json
00003 library 'web_server': Fnparse_Json
00004 !
00005 ! Note That Inside This Json, "codes" Is An Array Of Values
00006 let Json$ = '{ "Level":1, "oper": 41, "opername":"MZ", "codes": [10, 4, 32, 65] }'
00007 !
00008 ! This Means We Are Parsing An Object, Not An Array
00009 let Valuetype$ = '{'
00010 !
00011 ! Optional, But Must Be A Variable, We Cannot Just Pass An Empty String Literal '' To Fnparse_Json
00012 let Membername$ = ''
00013 !
00014 let Fnparse_Json (Json$, Valuetype$, Membername$, Mat Keys$, Mat Values$)
00015 !
00016 print 'Keys:'
00017 print Mat Keys$
00018 print 'Values:'
00019 print Mat Values$
Mikhail
Posts: 87
Joined: Tue Jul 07, 2009 10:26 am
Location: Ukraine

Re: Anybody written a JSON Parser?

Post by Mikhail »

This what the JSON looks like in "pretty" print:

{
"Level":1,
"oper":41,
"opername":"MZ",
"codes": [ 10, 4, 32, 65 ]
}

The initial opening curly brace { means that we are looking at an object.
On the contrary, "codes", a "key" inside of the object, has a value that is an array of numbers. We know this is an array because the value of "codes" begins with a bracket [
In short, JSON uses { } to denote objects and [ ] to denote arrays. Objects and arrays can be nested within each other.
bluesfannoz
Posts: 291
Joined: Fri Jun 19, 2009 9:01 am
Location: Lawrence, Kansas
Contact:

Re: Anybody written a JSON Parser?

Post by bluesfannoz »

Perfect! Just wanted I needed! Once again the forum shows its value!
Steve Koger
Computer Specialist
SEKESC-MACS Division
gordon
Posts: 358
Joined: Fri Apr 24, 2009 6:02 pm

Re: Anybody written a JSON Parser?

Post by gordon »

Thank you Mikhail for your excellent example.

In examining the sample Mikhail provided, I became interested in the Member_Name$ parameter. After checking it out I realized its role was unclear and it didn't perform in all cases the way I think it should. So I have updated both the JSON Parser (web_server.bro) and the sample program to enable people to experiment with a variety of JSON structures.

Here is the revised program, and I have attached the program plus an updated copy of the web_server:

01000 ! Rep Parse
01020 dim KEYS$(0)*255,VALUES$(0)*255
01040 dim JSON$*30000 !Max allowed by fnparse_json
01060 library 'web_server': FNPARSE_JSON
01080 !
01100 ! Note That Inside This Json, "codes" Is An Array Of Values
01120 let JSON$ = '{ "Level":1, "oper": 41, "opername":"MZ", "codes": [10, 4, 32, 65] }'
01140 ! Let Json$ = '{"name":"Mable"}'! 1 Simple Member
01160 ! Let Json$ = '{"group": {"name":"Mable"}}' ! 1 Complex Member
01180 ! Let Json$ = '{"name":"Mable","phone":"123-456-7890"}' !2 Members
01200 ! Let Json$ = '{"name":["Mable","phone","123-456-7890"]}' !1 Complex Member
01220 !
01240 ! The Parser Will Set This To No Keys ( [ ) Or Keys ( { )
01260 let VALUETYPE$ = ''
01280 !
01300 ! Must Be A Variable, We Cannot Just Pass A String Literal '' To Fnparse_Json
01320 let MEMBERNAME$ = ''
01340 !
01360 let RC = FNPARSE_JSON (JSON$, VALUETYPE$, MEMBERNAME$, MAT KEYS$, MAT VALUES$)
01380 if RC < 0 then print "Error ";RC;" occurred parsing JSON." !:
stop
01400 !
01420 print VALUETYPE$;" ";MEMBERNAME$
01440 print 'Keys:'
01460 print MAT KEYS$
01480 print 'Values:'
01500 print MAT VALUES$

It is intended to allow the user to progressively unremark the JSON value statements to see what the results will be for each structure. I have also added a print statement to display value type and member name.

Note that the reason these two parameters must be variables is so FNPARSE_JSON can return values in them. The data you pass to the parser in them is completely ignored. Also, it is a good idea to check for a negative return code from the library call. Parser return codes can be informative.

Member Name
The Member Name field only applies to complex single members. An example of a simple member is {"name": "Mable"}. A complex member example is {"group": {"name": "Mable"}}. See the program above for other examples. A simple member will return a null Member Name and the first key will contain the name.
Attachments
parse.brs
(1.2 KiB) Downloaded 735 times
web_server.bro
(47.21 KiB) Downloaded 676 times
gordon
Posts: 358
Joined: Fri Apr 24, 2009 6:02 pm

Re: Anybody written a JSON Parser?

Post by gordon »

Here is a similar program describing how FNCOMPILE_JSON works: (also attached)

01000 ! Rep Compile
01020 dim KEYS$(2)*255,VALUES$(2)*255,MEMBERNAME$*50
01040 dim JSON$*30000 !Max allowed by fnparse_json
01060 library 'web_server': FNPARSE_JSON,FNCOMPILE_JSON
01080 !
01100 ! Set This To List ( '[' - No Keys ) Or Member ( '{' - With Keys )
01120 let VALUETYPE$ = '['
01140 !
01160 ! Set This To Null Or The Name Of A Member
01180 ! If Null A List Will Be Created From Keys$ And Values$.
01200 ! If Non-Null A Member With This Name Will Be Created.
01220 let MEMBERNAME$ = ''
01240 !
01260 read VALUETYPE$,MEMBERNAME$,MAT KEYS$,MAT VALUES$
01280 ! ***** Members
01300 ! Data [,'',Name,Phone,Mable,123-456-7890
01320 ! Data {,'',Name,Phone,Mable,123-456-7890
01340 ! Data {,Group,Name,Phone,Mable,123-456-7890
01360 !
01380 ! ***** Lists
01400 ! Data [,'','','',Mable,123-456-7890
01420 ! The Presence Of A Non-Null Key$ Configures The Value As A Member.
01440 ! The Presence Of A Non-Null Membername Configures The List As
01460 ! The Value Of A Member.
01480 ! Data [,Group,'',Phone,Mable,123-456-7890
01500 ! Data [,'','','','',''
01520 ! Data {,'','','','','' !Fails
01540 !
01560 let RC = FNCOMPILE_JSON (VALUETYPE$, MEMBERNAME$, MAT KEYS$, MAT VALUES$,JSON$)
01580 if RC < 0 then print "Error ";RC;" occurred compiling JSON." !:
stop
01600 !
01620 print VALUETYPE$;" ";MEMBERNAME$
01640 print 'Keys:'
01660 print MAT KEYS$
01680 print 'Values:'
01700 print MAT VALUES$
01720 print JSON$

You will need to unremark a data statement to see the results.
Attachments
compile.brs
(1.52 KiB) Downloaded 720 times
bluesfannoz
Posts: 291
Joined: Fri Jun 19, 2009 9:01 am
Location: Lawrence, Kansas
Contact:

Re: Anybody written a JSON Parser?

Post by bluesfannoz »

Thanks for the example! Another good one would be showing how one would using the http=client upload their JSON data to a 3rd party web server?

I have seen how to parse the JSON I may receive, but have not seen an example of how on the client end to package up some JSON Data and send it over an https connection.

Steve
Steve Koger
Computer Specialist
SEKESC-MACS Division
Mikhail
Posts: 87
Joined: Tue Jul 07, 2009 10:26 am
Location: Ukraine

Re: Anybody written a JSON Parser?

Post by Mikhail »

I don't have a sample of using http=client, but I do have samples of uploading text and/or JSON data to a BR web server via POST, as well as getting data from a BR web server via GET. This is not the same as what you asked for, so not sure if this helps any. But if you are interested, I'll dig around for some good samples.
Post Reply