Author Topic: Parse JSON strings (not an API)  (Read 2735 times)

0 Members and 1 Guest are viewing this topic.

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Parse JSON strings (not an API)
« on: July 29, 2020, 09:33:11 am »
As the notice in parentheses in the title states, this is some code for parsing a JSON string and this code is not an API. So far, I've only tested it on JSON responses from my API projects. Those responses have zero whitespace which makes it easy to standardize this function. HOWEVER, other JSON files or strings from other sources might have whitespace and would not work properly with what this is. I'll end up copying all my API projects into one thread and updating their source codes with these functions so they work in a more friendly manner. I had tried to use Galleon's qb_framework and it was a total mess. No documentation that I could find anywhere and so I couldn't even begin to figure out how to translate a JSON to plain text with that. However, I will use it for generating JSON response files at some point because it can do that and it looks like it does it quite well. Anyways, enough rambling. Here is the code for my JSON parsing. I've attached three JSON files from API projects I've worked on so you can test both functions. GetKey will grab the first instance of that key. Useful for a file that has ONE response. GetAllKey will return an array containing all values for that keyname in the JSON file:

Code: QB64: [Select]
  1.  
  2.  
  3. 'OPEN "nasapod.json" FOR BINARY AS #1
  4. 'OPEN "sentimapi.json" FOR BINARY AS #1
  5. OPEN "addressvalidation.json" FOR BINARY AS #1
  6. JSON = SPACE$(LOF(1))
  7. GET #1, , JSON
  8.  
  9. 'PRINT GetKey("title", JSON) 'for use with nasapod
  10. 'PRINT GetKey("url", JSON) 'for use with nasapod
  11. 'PRINT GetKey("explanation", JSON) 'for use with nasapod
  12. 'REDIM Polarity(0) AS STRING 'for use with sentimapi
  13.  
  14. PRINT GetKey("delivery_line_1", JSON) 'for use with adddressvalidation
  15. PRINT GetKey("last_line", JSON) 'for use with addressvalidation
  16. PRINT GetKey("county_name", JSON) 'for use with addressvalidation
  17.  
  18. 'GetAllKey "polarity", JSON, Polarity() 'for use with sentimapi
  19.  
  20. 'DIM i
  21. 'FOR i = 1 TO UBOUND(Polarity) 'for use with sentimapi
  22. '    PRINT Polarity(i)
  23. 'NEXT
  24.  
  25. FUNCTION GetKey$ (keyname AS STRING, JSON AS STRING)
  26.     DIM jkey AS STRING
  27.     IF INSTR(JSON, CHR$(34) + keyname + CHR$(34)) THEN
  28.         jkey = MID$(JSON, INSTR(JSON, CHR$(34) + keyname + CHR$(34)) + LEN(keyname))
  29.         jkey = MID$(jkey, INSTR(jkey, ":") + 1)
  30.         IF MID$(jkey, 1, 1) = CHR$(34) THEN
  31.             jkey = MID$(jkey, 2)
  32.         END IF
  33.         jkey = MID$(jkey, 1, INSTR(jkey, CHR$(34)) - 1)
  34.         IF RIGHT$(jkey, 1) = "," THEN
  35.             jkey = MID$(jkey, 1, LEN(jkey) - 1)
  36.         END IF
  37.     ELSE
  38.         GetKey = ""
  39.     END IF
  40.     GetKey = jkey
  41.  
  42. SUB GetAllKey (keyname AS STRING, JSON AS STRING, ParseKey() AS STRING)
  43.     DIM jkey AS STRING
  44.     DIM x
  45.     DO
  46.         IF INSTR(JSON, CHR$(34) + keyname + CHR$(34)) THEN
  47.             x = x + 1
  48.             REDIM _PRESERVE ParseKey(x) AS STRING
  49.             JSON = MID$(JSON, INSTR(JSON, CHR$(34) + keyname + CHR$(34)) + LEN(keyname))
  50.             jkey = JSON
  51.             jkey = MID$(jkey, INSTR(jkey, ":") + 1)
  52.             IF MID$(jkey, 1, 1) = CHR$(34) THEN
  53.                 jkey = MID$(jkey, 2)
  54.             END IF
  55.             jkey = MID$(jkey, 1, INSTR(jkey, CHR$(34)) - 1)
  56.             IF RIGHT$(jkey, 1) = "," THEN
  57.                 jkey = MID$(jkey, 1, LEN(jkey) - 1)
  58.             END IF
  59.             ParseKey(x) = jkey
  60.         END IF
  61.     LOOP UNTIL INSTR(JSON, CHR$(34) + keyname + CHR$(34)) = 0
* sentimapi.json (Filesize: 0.13 KB, Downloads: 147)
* nasapod.json (Filesize: 1.29 KB, Downloads: 141)
* addressvalidation.json (Filesize: 0.89 KB, Downloads: 154)
« Last Edit: July 29, 2020, 09:45:40 am by SpriggsySpriggs »
Shuwatch!