Page 1 of 1

LoadJSON confusion on my part.

Posted: Fri Dec 28, 2018 5:36 pm
by HwyStar
The PB help text for LoadJSON() does not have an example of it anywhere in the system...

In the following code I continue to get the error: "A JSON value of type array is expected". What am I doing wrong?

The token.txt file has the data in it that you see commented at the top of my source code. The program breaks at line 21: ExtractJSONList(JSONValue(0), Token())

Code: Select all

; File: token.txt
;{
;  "token_type": "BearerToken",
;  "issued_at": "1545950151716",
;  "scope": "",
;  "expires_in": 3599,
;  "access_token": "VXqAKoCDUiNFlsDqGorAP77LVrEI"
;}

Structure TokenS
  Token_Type.s
  Issued_At.s
  Scope.s
  Expires_In.i
  Access_Token.s
EndStructure 

Global NewList Token.TokenS()

If LoadJSON(0, "z:\api\source\token.txt") 
  ExtractJSONList(JSONValue(0), Token())                  ; breaks here!
  FreeJSON(0)                             
Else
  Debug "Error reading file: " + JSONErrorMessage() 
EndIf

ForEach Token()
  Debug(Token()\Access_Token)
Next Token()
Thanks for your help in advance!

Re: LoadJSON confusion on my part.

Posted: Fri Dec 28, 2018 6:01 pm
by Marc56us
1. Need [ ] for a JSON list even with one element only

Code: Select all

[
	{
	  "token_type": "BearerToken",
	  "issued_at": "1545950151716",
	  "scope": "",
	  "expires_in": 3599,
	  "access_token": "VXqAKoCDUiNFlsDqGorAP77LVrEI"
	}
]
2. JSON is Case sensitive, so for your example, add #PB_JSON_NoCase

Code: Select all

LoadJSON(0, "token.txt", #PB_JSON_NoCase)

Code: Select all

; File: token.txt

; [
;   {
;       "token_type": "BearerToken",
;       "issued_at": "1545950151716",
;       "scope": "",
;       "expires_in": 3599,
;       "access_token": "VXqAKoCDUiNFlsDqGorAP77LVrEI"
;   }
; ]

Structure TokenS
  Token_Type.s
  Issued_At.s
  Scope.s
  Expires_In.i
  Access_Token.s
EndStructure

Global NewList Token.TokenS()

If LoadJSON(0, "token.txt", #PB_JSON_NoCase)
    ExtractJSONList(JSONValue(0), Token())        
  FreeJSON(0)                             
Else
  Debug "Error reading file: " + JSONErrorMessage()
EndIf

ForEach Token()
  Debug(Token()\Access_Token)
Next Token()

Code: Select all

VXqAKoCDUiNFlsDqGorAP77LVrEI
:wink:

Re: LoadJSON confusion on my part.

Posted: Fri Dec 28, 2018 6:06 pm
by HwyStar
Thanks for the quick solution Marc!

My token.txt file will not have the brackets in the file. Just the "{" and "}" characters or a standard JSON reply file. Is there an easier way to read a JSON reply file that I am missing?

If I do use the LoadJSON procedure then I will add the #PB_JSON_NoCase parameter.

Re: LoadJSON confusion on my part.

Posted: Fri Dec 28, 2018 6:11 pm
by Marc56us
The PB LoadJson() function seems to want the [ ]

A simple solution would be to rewrite the input file into a temporary file or into memory and prefix it with [ and suffix it with a ] before parsing it with LoadJson().

:)

Re: LoadJSON confusion on my part.

Posted: Fri Dec 28, 2018 6:18 pm
by HwyStar
That was my thought too: Open the token file and add brackets to the output to a temp file and read the temp file using LoadJSON. That's not very graceful but it would work.

Should I consider using the LoadJSONStructure() procedure instead? But I bet it is looking for the brackets too...

Thanks, Marc!