1. Matt Balent
  2. PowerBuilder
  3. Friday, 21 October 2022 17:37 PM UTC

So I have a JSON string I'd like to load into a JSONpackage object for further processing.  Problem is (apparently) it is an array which I guess the JSONPackage object cannot handle.  I get this error: Failed to load the JSON data because its root node is not an object

So, how do I process the file?  I assume some kind of loop through the JSON?

Code I tried:

ls_mess = ljs_pack1.loadstring(ls_pack_json) //Gives the "Failed to load the JSON data..." if the JSON is an array.
ls_jpk_json = ljs_pack1.GetValue("_patientCaseLimited") //If ls_pack_json is not an array this works since it is successfully loaded.

Link to JSON file: 

https://www.dropbox.com/s/u54ic1jb1pdgl4o/sample.json?dl=0

 Thank you

UPDATE with Solution

Here is what I ended up with to solve my particular problem.

The data is an array which has a second array (called '_patientCaseLimited') and it is this piece I need.

//ljs_pack is a JSONPackage object

//ljs_parser is a JSONParser object

ls_pack_json = ljs_pack.GetValue("Data") // gets the JSON data

ls_message = ljs_parser.loadstring(ls_pack_json)

ll_arrayitem = ljs_parser.getrootitem() // start at the root

ll_childcount = ljs_parser.getchildcount(ll_arrayitem) // how many child elements?

FOR ll_i = 1 to ll_childcount

     ll_objectitem = ljs_parser.getchilditem(ll_arrayitem, ll_i) //next child array element

     ls_casejson += ljs_parser.getitemarrayjsonstring(ll_objectitem, "_patientCaseLimited") //append arrays

NEXT

ls_jpk_json = lnv_string.of_globalreplace(ls_casejson, '][', ',') //PFC String Service

 

Now I can import the ls_jpk_json into a datawindow

Ken Guo @Appeon Accepted Answer Pending Moderation
  1. Monday, 24 October 2022 02:53 AM UTC
  2. PowerBuilder
  3. # 1

Hi Matt,

Your Sample.Json file is an array. JasonPackage does not support directly loading an array and will prompt "Failed to load the JSON data because its root node is not an object".
I suggest you try adding a Node to the Json, for example:
ls_Error = lnv_JsonPackage.LoadString('{ "SampleData":' + ls_pack_json+ '}')

Of course, if you simply want to parse the content in Json, I recommend using the JsonParse object. JsonParse can load this sample.json file directly. For details, you can refer to the example provided by Arnd.


Regards,
Ken

Comment
There are no comments made yet.
Arnd Schmidt Accepted Answer Pending Moderation
  1. Friday, 21 October 2022 19:39 PM UTC
  2. PowerBuilder
  3. # 2

Hi Matt, my bad . You are correct... that does not work :-(

After I changed all the "_patientCaseLimited" to "patientCaseLimited" (without an underscore) in the json file, the loadString and loadFile works.
So it seems to be a bug / feature in PowerBuilder's JSON Implementation :-(

JsonParser lnv_JsonParser 
integer li_fileno
String ls_Json, ls_Error
string ls_filename = "D:\asc\PB2019\pb2019playground\sample_edited.json"

li_fileNo = FileOpen (ls_filename)
FileReadEx ( li_fileNo, ls_Json )
FileClose ( li_fileNo )

lnv_JsonParser = Create JsonParser
ls_Error = lnv_JsonParser.LoadString( ls_Json )
ls_Error = lnv_JsonParser.LoadFile( ls_filename)

return

 

Edit:

Only JSONParser supports your kind of data. JSONPackage only supports "plain JSON".

regards

Arnd

Attachments (1)
Comment
  1. Matt Balent
  2. Friday, 21 October 2022 20:06 PM UTC
Still not working. I should have said I am using PB2019R3.

If I remove the underscore from the example data I get the same errors. If I put curly braces around the JSON I get "Missing a name for object member" error.
  1. Helpful
  1. Arnd Schmidt
  2. Friday, 21 October 2022 20:39 PM UTC
Not sure what happened. I added the edited json and my sample code. At least no error on loading the json.
  1. Helpful
  1. Arnd Schmidt
  2. Friday, 21 October 2022 21:55 PM UTC
Ah.. ok. you use the JSONPackage. This only seems to support "plain json".

https://docs.appeon.com/pb2019/application_techniques/ch18s01.html

  1. Helpful
There are no comments made yet.
Arnd Schmidt Accepted Answer Pending Moderation
  1. Friday, 21 October 2022 17:57 PM UTC
  2. PowerBuilder
  3. # 3

Hi Matt,

I think you just need some curly braces around your JSON :

ls_mess = ljs_pack1.loadstring("{ " + ls_pack_json + " }")

hth

Arnd

 

Comment
  1. Matt Balent
  2. Friday, 21 October 2022 18:15 PM UTC
nope, same errors.
  1. Helpful
  1. Arnd Schmidt
  2. Friday, 21 October 2022 19:43 PM UTC
Outch... Sorry for not testing it before.
  1. Helpful
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.