1. Kiat Chun
  2. PowerBuilder
  3. Tuesday, 22 January 2019 02:18 AM UTC

Hi team,

This question is a follow-up from my previous question about how to make a API calls, which is now possible.

I received following API string, and assigning it to a string variable.

ls_stringbody = '{"addresses":[{"id":2521795,"name":"John Smith","company":"Company Ltd","building":"Building Name","street":"Street Name","suburb":"Suburb Name","post_code":"1000","city":"City Name","state":"NSW","country":"Australia","code":"123","telephone":"012345678","email":"email@testemail.com"},{"id":2521796,"name":"Jane Smith","company":"Company Ltd","building":"Building Name","street":"Street Name","suburb":"Suburb Name","post_code":"2000","city":"City Name","state":"NSW","country":"Australia","code":"123","telephone":"987654321","email":"email@testemail.com"}]}'

 

Now I would like to parse this information into say, a datastore/datawindow, and vice-versa.. get modified data out of datawindow and submit back to API call.

 

Which powerscript functions I need to parse JSON string into datastore, and vice-versa ...back to API call.

I've tried several samples from PowerScript reference, but I keep getting error message:

"The returned data does not match the item data type."

Maybe I am looking at the wrong example.

 

The steps I've taken:

//1. Create json parser
JSONParser json
json = CREATE JSONParser

//2. Loads JSON String... and Check for irregular JSON string contain null value
ls_result = json.LoadString (ls_stringbody)
IF Len (Trim (ls_result)) > 0 THEN
   MessageBox ("Failed","Load JSON Failed:" + ls_result)
 RETURN
END IF

//3. GetRootItem
ll_root = json.GetRootItem ()      // this always returns 1

//4. GetChildCount
ll_count = json.GetChildCount (ll_root)     //this always returns 1

//5. GetChildItem using a LOOP
FOR ll_index = 1 TO ll_count
    ll_row = dw_1.InsertRow (0)
    ll_child = json.GetChildItem (ll_root, ll_index) 
    ll_id = json.GetitemNumber (ll_child, "id")
    dw_1.SetItem (ll_row, "c1", String (ll_id))
    ls_name = json.GetItemString (ll_child, "name")
    dw_1.SetItem (ll_row, "c2", ls_name)
    ls_company = json.GetItemString (ll_child, "company")
    dw_1.SetItem (ll_row, "c3", ls_company)

NEXT

 

But I keep getting an error message "The returned data type does not match the item data type."

 

Appreciate your advise.

Thanks in advance

Kiat

Accepted Answer
Chris Pollach @Appeon Accepted Answer Pending Moderation
  1. Wednesday, 23 January 2019 18:49 PM UTC
  2. PowerBuilder
  3. # Permalink

Hi Kiat;

  All fixed!  ;-)

HTH

Regards ... Chris

 

Attachments (1)
Comment
  1. Kiat Chun
  2. Thursday, 24 January 2019 04:07 AM UTC
Wow! thank you Chris. Appreciate your help.



So we use GetChildItem to get the handle for secondary root first, before using GetChildItem again to get the handle for each row, in a loop.



  1. Helpful
  1. Chris Pollach @Appeon
  2. Thursday, 24 January 2019 04:14 AM UTC
Correct. Your JSON code has to follow the schema structure.
  1. Helpful
  1. Chris Pollach @Appeon
  2. Thursday, 24 January 2019 21:14 PM UTC
FWIW: When it comes to quickly looking at JSON datum, FireFox is your friend.

It's JSON debugging and parsing features are most impressive (IMHO). ;-)
  1. Helpful
There are no comments made yet.
Kiat Chun Accepted Answer Pending Moderation
  1. Wednesday, 23 January 2019 03:19 AM UTC
  2. PowerBuilder
  3. # 1

Hi Chris,

Attached is sample PBL. For simplicity and learning purpose, I've placed all the codes into a single script.

It is only accessing demo site & I will regenerate the key once this issue is resolved.

Thanks

Attachments (1)
Comment
There are no comments made yet.
Chris Pollach @Appeon Accepted Answer Pending Moderation
  1. Tuesday, 22 January 2019 20:29 PM UTC
  2. PowerBuilder
  3. # 2

Hi Kiat;

  Can you attach the JSON data stream and the exported DWO source that you are using in the above example code?

Regards ... Chris

Comment
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.