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
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.
It's JSON debugging and parsing features are most impressive (IMHO). ;-)