LoadJSON confusion on my part.

Everything else that doesn't fall into one of the other PB categories.
User avatar
HwyStar
Enthusiast
Enthusiast
Posts: 101
Joined: Mon Apr 05, 2010 7:13 pm
Location: Reno, Nevada

LoadJSON confusion on my part.

Post 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!
Marc56us
Addict
Addict
Posts: 1600
Joined: Sat Feb 08, 2014 3:26 pm

Re: LoadJSON confusion on my part.

Post 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:
User avatar
HwyStar
Enthusiast
Enthusiast
Posts: 101
Joined: Mon Apr 05, 2010 7:13 pm
Location: Reno, Nevada

Re: LoadJSON confusion on my part.

Post 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.
Marc56us
Addict
Addict
Posts: 1600
Joined: Sat Feb 08, 2014 3:26 pm

Re: LoadJSON confusion on my part.

Post 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().

:)
User avatar
HwyStar
Enthusiast
Enthusiast
Posts: 101
Joined: Mon Apr 05, 2010 7:13 pm
Location: Reno, Nevada

Re: LoadJSON confusion on my part.

Post 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!
Post Reply