BASIC User Group > Code Challenges

JSON Challenge

(1/11) > >>

AIR:
Time for a new challenge:

Using the attached json file, which was pulled from github and shows commit status, show how your language of choice would extract the following fields:


* Name of Commiter
* Date of Commit
* The SHA HASH for that Commit
Using Libraries/Built in JSON support is allowed; I thought about restricting this to only "roll your own" implementations, but wanted to make this challenge accessible to a wider audience, since coding a home-made parser might be beyond the reach of most, especially time-wise.

AIR.

John:
I'm going to try native SB syntax to create a BASIC only extension json module. Using an external library for something based on a limited definition seems like overkill to me. My SB XML parser was less than 10 lines.

AIR:

--- Quote from: John on November 24, 2018, 04:44:19 pm ---I'm going to try native SB syntax to create a BASIC only extension json module. Using an external library for something based on a limited definition seems like overkill to me. My SB XML parser was less than 10 lines.

--- End quote ---

Where simple text parsers become not so simple is when you have to handle deeply nested "objects", malformed data sets, and validation of those sets. Also you may need to account for UTF-8.

JSON is not just about brackets; there's a bunch of other things that the official spec supports.

With all that said, go for it! 

AIR.

AIR:
One of my personal goals with this challenge is to implement an interface that makes it easy to grab the data you need.

For me, it's not only about can the challenge be met, but also about how simple it is for a programmer to use what you've created for other purposes.

With that said, he's what I worked up this evening, using HOTBASIC:


--- Code: Text ---'-------------------------------------------------------' DATE: 11/24/2018' NAME: parse.bas' DESCRIPTION: JSON Parsing demo, using libparson' AUTHOR:  Armando I. Rivera (AIR)' LANGUAGE:  HOTBASIC! v7.1' EDITOR:         AIRSyS-IDE'------------------------------------------------------- $apptype console: $typecheck on $include "JSON.bas" deflng item, idim json as JSON json.init("commits.json") if json.type = JSONArray then        for i = 0 to json.count-1                item = json.object(i)                print string$(50,"-")                print "Name: "; json.text(item,"commit.author.name")                print "Commit Date: "; json.text(item, "commit.author.date")                print "SHA Hash: "; json.text(item, "sha")        next        print string$(50,"-")end if end 
The JSON.bas file consists of:

--- Code: Text ---'-------------------------------------------------------'   NAME:                       JSON.bas'   Version:            1.0'   DATE:                       2018-11-24'-------------------------------------------------------' DEVELOPMENT ENVIRONMENT'   OpSys:                      Windows'   Language:   HotBasic'   Editor:                     AIRSyS-IDE'   Libraries:          libparson.dll'-------------------------------------------------------' AUTHOR:               Armando I. Rivera (AIR)'-------------------------------------------------------' DESCRIPTION:'                                               libparson Json parser interface''-------------------------------------------------------' Terms of Use/Licence'                                               MIT'------------------------------------------------------- declare sub JSONinit (filename as string)declare function JSONtext(json_object as long,query as string) as stringdeclare function JSONobject(index as long) as long OBJECT JSON        root as long        items as long        count as long        type as long        text as Function        object as function        init as SubEND OBJECT ' JSON VALUE TYPE CODESCONST    JSONError      =       -1CONST    JSONNull               =       1CONST    JSONString     =       2CONST    JSONNumber     =       3CONST    JSONObject     =       4CONST    JSONArray      =       5CONST    JSONBoolean     =      6 begin runonce        cdecl function json_parse_file lib "libparson.dll"   (filename as string) as long        cdecl function json_parse_string lib "libparson.dll" (strBuffer as string) as long        cdecl function json_value_get_type  lib "libparson.dll"   (json_object as long) as long        cdecl function json_value_get_array  lib "libparson.dll"   (json_object as long) as long        cdecl function json_array_get_count  lib "libparson.dll"   (json_object as long) as long        cdecl function json_object_get_string lib "libparson.dll" (json_object as long, keyname as string) as string        cdecl function json_object_dotget_string lib "libparson.dll"  (json_object as long, keypath as string) as string        cdecl function json_array_get_object lib "libparson.dll" (json_object as long, index as long) as long        cdecl function json_object_dotget_value lib "libparson.dll" (json_object as long, keypath as string) as long        cdecl function json_object_get_value lib "libparson.dll" (json_object as long, keypath as string) as longend runonce Sub JSONinit(filename as string)        self.root = json_parse_file(filename)        self.type = json_value_get_type(self.root)        if self.type = JSONArray then                self.items = json_value_get_array(self.root)                self.count = json_array_get_count(self.items)        end ifEnd Sub function JSONtext(json_object as long, query as string) as string        defstr res                if instr(query, ".") then                if  json_object_dotget_value(json_object,query) <> 0 then                        res = json_object_dotget_string(json_object, query)                end if'               print res        else                if  json_object_get_value(json_object,query) <> 0 then                        res = json_object_get_string(json_object, query)                end if        end if                result = resend function function JSONobject(index as long) as long        result =  json_array_get_object(self.items, index)end function  
Attached you find the output, and the 32bit dll (HOTBASIC is 32bit only)

AIR.

John:
Script BASIC


--- Code: Script BASIC ---OPEN "commits.json" FOR INPUT AS #1fl = FILELEN("commits.json")json = INPUT(fl,1)SPLITA json BY _"""  {    "sha": """ _TO commitsFOR i = 1 TO UBOUND(commits)  IF commits[i] LIKE """*committer*"name": "*"*date": "*"*""" THEN    PRINT JOKER(3),"\n"    PRINT JOKER(5),"\n"    PRINT MID(commits[i],2,40),"\n"    PRINT STRING(40,"="),"\n"  END IFNEXT 

jrs@jrs-laptop:~/sb/abcc/json$ time scriba jsoncc.sb
Linus Torvalds
2018-11-24T20:58:47Z
e195ca6cb6f21633e56322d5aa11ed59cdb22fb2
========================================
Linus Torvalds
2018-11-24T17:42:32Z
d146194f31c96f9b260c5a1cf1592d2e7f82a2e2
========================================
Linus Torvalds
2018-11-24T17:19:38Z
857fa628bbe93017c72ddd0d5304962a2608db07
========================================
Linus Torvalds
2018-11-24T17:11:52Z
abe72ff4134028ff2189d29629c40a40bee0a989
========================================
David S. Miller
2018-11-24T06:35:38Z
07093b76476903f820d83d56c3040e656fb4d9e3
========================================
David S. Miller
2018-11-24T06:34:40Z
3fa528b7682e73e906266bcd43728b8f923bf9b2
========================================
David S. Miller
2018-11-24T06:33:55Z
e7b9fb4f545b1f7885e7c642643828f93d3d79c9
========================================
David S. Miller
2018-11-24T06:31:56Z
ef2a7cf1d8831535b8991459567b385661eb4a36
========================================
David S. Miller
2018-11-24T01:24:24Z
c44c749d3b6fdfca39002e7e48e03fe9f9fe37a3
========================================
David S. Miller
2018-11-24T01:18:15Z
5ed9dc99107144f83b6c1bb52a69b58875baf540
========================================
David S. Miller
2018-11-23T19:59:40Z
18ba58e1c234ea1a2d9835ac8c1735d965ce4640
========================================
David S. Miller
2018-11-23T19:59:40Z
e59ff2c49ae16e1d179de679aca81405829aee6c
========================================
Linus Torvalds
2018-11-23T19:24:55Z
7c98a42618271210c60b79128b220107d35938d9
========================================
Linus Torvalds
2018-11-23T19:20:14Z
3381918fec9278d14f776d1dabd68da85fd6822e
========================================
David S. Miller
2018-11-23T19:20:02Z
484afd1bd3fc6f9f5347289fc8b285aa65f67054
========================================
David S. Miller
2018-11-23T19:18:53Z
605108acfe6233b72e2f803aa1cb59a2af3001ca
========================================
David S. Miller
2018-11-23T19:17:56Z
896585d48e8e9ba44cd1754fbce8537feffcc1a5
========================================
Linus Torvalds
2018-11-23T19:15:27Z
d88783b9c8849d88c3a75b7b9071cba072b47eba
========================================
David S. Miller
2018-11-23T19:08:03Z
5cd8d46ea1562be80063f53c7c6a5f40224de623
========================================
Linus Torvalds
2018-11-23T18:56:16Z
a03bac580ae743d5900af626ac63f7f8cd85def9
========================================
Linus Torvalds
2018-11-23T18:52:57Z
b88af994872418f0a98db6f4a9bae849315a99b0
========================================
Catalin Marinas
2018-11-23T18:44:16Z
4f9f49646a5733c0c2bd49940673dde89a9c5add
========================================
Linus Torvalds
2018-11-23T18:40:19Z
e6005d3c42336074de3745718ac85807dd6e1e6a
========================================
Linus Torvalds
2018-11-23T18:36:02Z
dcd3aa31dcdd6d8eae8d4771c44aeb3b1fec995a
========================================
Linus Torvalds
2018-11-23T18:03:08Z
9b7c880c834c0a1c80a1dc6b8a0b19155361321f
========================================
Catalin Marinas
2018-11-23T17:33:27Z
b5d9a07ef7736b2456b9d3c90568de25e43d8ec3
========================================
Rafael J. Wysocki
2018-11-23T09:32:49Z
1d50088ca3956e5dcd2751a658e7869b9af10bb4
========================================
Rafael J. Wysocki
2018-11-23T09:32:22Z
bec00cb5e97c19d2c8bd10db15d334cb40760000
========================================
Dave Airlie
2018-11-23T01:03:21Z
98c9cdfd34fbb62886e4c5a07e33661aa3352ef5
========================================
David S. Miller
2018-11-22T19:53:26Z
039e70a70c8417b5bf5878a60612ebd2c95f731e
========================================

real   0m0.027s
user   0m0.015s
sys   0m0.011s
jrs@jrs-laptop:~/sb/abcc/json$


Navigation

[0] Message Index

[#] Next page

Go to full version