ImportJson
method (DataWindows)
Description
Inserts data from a JSON string into a DataWindow control, DataStore
object, or DataWindowChild object.
The JSON string must comply with the simple or standard format.
ImportJson function will fail to import data properly, if the
DataWindow is in query mode.
Applies to
|
DataWindow type |
Method applies to |
|---|---|
|
PowerBuilder |
DataWindow control, DataWindowChild object, and |
Syntax
PowerBuilder
|
1 |
long dwcontrol.ImportJson( string json {, string error} {, DWBuffer dwbuffer {, long startrow {, long endrow {, long startcolumn {, long endcolumn {, long dwstartcolumn } } } } } } ) |
|
Argument |
Description |
|---|---|
|
dwcontrol |
A reference to a DataWindow control, DataStore, or |
|
json |
A string specifying the JSON data. The JSON string |
|
error (optional) |
A variable into which the returned warning or error When there are a large amount The import warning caused by data type The import error Most of the messages |
|
dwbuffer (optional) |
A value of the dwBuffer enumerated datatype For For standard (DataWindow) JSON: If |
|
startrow (optional) |
The number of the first detail object in the JSON |
|
endrow (optional) |
The number of the last detail object in the JSON |
|
startcolumn (optional) |
The number of the first key value in the JSON object |
|
endcolumn (optional) |
The number of the last key value in the JSON object |
|
dwstartcolumn (optional) |
The number of the first column in the DataWindow |
Return value
Long. Returns the number of rows that were imported if it succeeds
and one of the following negative integers if an error occurs. The return
value will count the rows imported into the primary, filter, and delete
buffers, but not the rows imported into DataWindowChild.
0 — When all of the data in the JSON string is null or the JSON
string only contains data for DataWindowChild.
-1 — General error.
-2 — No row is supplied or the startrow value supplied is greater
than the number of rows in the JSON data.
-3 — Invalid argument.
-4 — Invalid JSON.
-5 — JSON format error.
-6 — Unsupported DataWindow presentation style for import.
-7 — Error resolving DataWindow nesting.
-8 — Unsupported mapping-method value.
The method returns null if any of the following:
-
any argument’s value is null
-
the DataWindow object (dataobject) is invalid
Usage
There is no forced conversion between strings and numbers. For
example, the number 123 in JSON string will not be imported into the
DataWindow column of char(10) type. For such case, a data type mismatch
warning will be recorded in the error argument.
A boolean value (true or false) will be converted to 0 or 1 when
imported from the JSON string to the DataWindow; however, 0 or 1 will not
be converted to a boolean value (true or false) when exported from the
DataWindow to the JSON string.
If the string length in JSON is larger than the string length in
DataWindow, the string will be truncated when imported into the
DataWindow. For example, JSON string [{“name”:”TestForTrancate”}] is
imported as “Test” when the data type of DataWindow column “name” is
char(4).
When the number value is imported from the JSON string to the
DataWindow column of number data type (with uncertain precision), the
value will have uncertain decimals, for example, 6.78 becomes
6.78000020980835 after imported from the JSON string to the
DataWindow.
Examples
This example gets data of simple-format JSON format from the server
and imports into the primary buffer of the DataWindow:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
int li_rc long ll_RowCount string ls_SimpleJson HttpClient lnv_HttpClient lnv_HttpClient = create HttpClient // send request using GET method li_rc=lnv_HttpClient.SendRequest("GET","http://demo.appeon.com/PB/webapi_client/employee/102") // obtain the response data if li_rc = 1 and lnv_HttpClient.GetResponseStatusCode() = 200 then lnv_HttpClient.GetResponseBody(ls_SimpleJson) ll_RowCount = dw_1.ImportJson(ls_SimpleJson) end if |
This example imports data (and state) from a standard-format JSON
string into the DataWindowChild and all of the buffers of the DataWindow
(see an example of a standard-format
JSON string):
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
long ll_RowCount string ls_StandardJson, ls_Error //Standard JSON ls_StandardJson = … ll_RowCount = dw_1.ImportJson(ls_StandardJson, ls_Error) //Checks if any error IF isnull(ll_RowCount) Then Messagebox("Error", "The method returns null") ElseIf ll_RowCount < 0 Then If len(ls_Error) > 0 Then Messagebox("Failed","Return Value: " + String(ll_RowCount) & + "~r~nWith error information:~r~n" + ls_Error) Else Messagebox("Failed","Return Value: "+String(ll_RowCount)) End If Else //Checks if any warning If len(ls_Error) > 0 Then MessageBox("Warning", "With warning information:~r~n" + ls_Error) Else MessageBox("Succeed", "Return Value: " + String(ll_RowCount) ) End If End If |
This example imports data (and state) from a standard-format JSON
string into the primary buffer of the DataWindow (see an example of a standard-format JSON
string):
|
1 2 3 4 5 6 |
long ll_RowCount string ls_StandardJson //Standard JSON ls_StandardJson = … ll_RowCount = dw_1.ImportJson(ls_StandardJson, Primary!) |
This example imports data from a simple-format JSON string in rows 2
through the end into the primary buffer of the DataWindow:
|
1 2 3 4 5 6 7 8 |
long ll_RowCount string ls_SimpleJson //Simple JSON ls_SimpleJson = '[{"department_id":100,"department_name":"Sales"}, & {"department_id":200,"department_name":"Finance"},{"department_id":300,"department_name":"Marketing"}]' ll_RowCount = dw_1.ImportJson(ls_SimpleJson, Primary!, 2) |
This example imports data from a simple-format JSON string in rows 1
through 3 into the filter buffer of the DataWindow:
|
1 2 3 4 5 6 7 8 9 10 |
long ll_RowCount string ls_SimpleJson //Simple JSON ls_SimpleJson = '[{"emp_id":1,"emp_fname":"Fran","emp_lname":"Whitney"}, & {"emp_id":2,"emp_fname":"Matthew","emp_lname":"Cobb"}, & {"emp_id":3,"emp_fname":"Philip","emp_lname":"Chin"}, & {"emp_id":4,"emp_fname":"Julie","emp_lname":"Jordan"}, & {"emp_id":5,"emp_fname":"Robert","emp_lname":"Breault"}]' ll_RowCount = dw_1.ImportJson(ls_SimpleJson, Filter!, 1, 3) |
This example imports data from a simple-format JSON string in rows 1
through 3 and in columns 2 through the end into the primary buffer of the
DataWindow:
|
1 2 3 4 5 6 7 8 9 10 |
long ll_RowCount string ls_SimpleJson //Simple JSON ls_SimpleJson = '[{"emp_id":1,"emp_fname":"Fran","emp_lname":"Whitney"}, & {"emp_id":2,"emp_fname":"Matthew","emp_lname":"Cobb"}, & {"emp_id":3,"emp_fname":"Philip","emp_lname":"Chin"}, & {"emp_id":4,"emp_fname":"Julie","emp_lname":"Jordan"}, & {"emp_id":5,"emp_fname":"Robert","emp_lname":"Breault"}]' ll_RowCount = dw_1.ImportJson(ls_SimpleJson, Primary!, 1, 3, 2) |
This example imports data (and state) from a standard-format JSON
string in rows 1 through 10 and in columns 1 through 5 into the primary
buffer of the DataWindow (see an
example of a standard-format JSON string):
|
1 2 3 4 5 6 |
long ll_RowCount string ls_StandardJson //Standard JSON ls_StandardJson = … ll_RowCount = dw_1.ImportJson(ls_StandardJson, Primary!, 1, 10, 1, 5) |
This example imports data from a simple-format JSON string in rows 1
through 3 and in columns 2 through 5 into the primary buffer of the
DataWindow beginning in column 3:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
long ll_RowCount string ls_SimpleJson //Simple JSON ls_SimpleJson = '[{"emp_id":1,"emp_fname":"Fran","emp_lname":"Whitney", & "street":"9 East Washington Street","city":"Cornwall"}, & {"emp_id":2,"emp_fname":"Matthew","emp_lname":"Cobb", & "street":"7 Pleasant Street","city":"Grimsby"}, & {"emp_id":3,"emp_fname":"Philip","emp_lname":"Chin", & "street":"539 Pond Street","city":"Oakville"}, & {"emp_id":4,"emp_fname":"Julie","emp_lname":"Jordan", & "street":"1244 Great Plain Avenue","city":"Woodbridge"}, & {"emp_id":5,"emp_fname":"Robert","emp_lname":"Breault", & "street":"358 Cherry Street","city":"Milton"}]' ll_RowCount = dw_1.ImportJson(ls_SimpleJson, Primary!, 1, 3, 2, 5, 3) |
This example imports data (and state) from a standard-format JSON
string into the primary buffer of the DataWindow (see an example of a standard-format JSON
string):
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
long ll_RowCount string ls_StandardJson, ls_Error //Standard JSON ls_StandardJson = … ll_RowCount = dw_1.ImportJson(ls_StandardJson, ls_Error, Primary!) //Checks if any error IF isnull(ll_RowCount) Then Messagebox("Error", "The method returns null") ElseIf ll_RowCount < 0 Then If len(ls_Error) > 0 Then Messagebox("Failed","Return Value: " + String(ll_RowCount) & + "~r~nWith error information:~r~n" + ls_Error) Else Messagebox("Failed","Return Value: "+String(ll_RowCount)) End If Else //Checks if any warning If len(ls_Error) > 0 Then MessageBox("Warning", "With warning information:~r~n" + ls_Error) Else MessageBox("Succeed", "Return Value: " + String(ll_RowCount) ) End If End If |
This example imports data from a simple-format JSON string in rows 2
through the end into the delete buffer of the DataWindow:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
long ll_RowCount string ls_SimpleJson, ls_Error //Simple JSON ls_SimpleJson = '[{"emp_id":1,"emp_fname":"Fran","emp_lname":"Whitney"}, & {"emp_id":2,"emp_fname":"Matthew","emp_lname":"Cobb"}, & {"emp_id":3,"emp_fname":"Philip","emp_lname":"Chin"}, & {"emp_id":4,"emp_fname":"Julie","emp_lname":"Jordan"}, & {"emp_id":5,"emp_fname":"Robert","emp_lname":"Breault"}]' ll_RowCount = dw_1.ImportJson(ls_SimpleJson, ls_Error, Delete!, 2) //Checks if any error IF isnull(ll_RowCount) Then Messagebox("Error", "The method returns null") ElseIf ll_RowCount < 0 Then If len(ls_Error) > 0 Then Messagebox("Failed","Return Value: " + String(ll_RowCount) & + "~r~nWith error information:~r~n" + ls_Error) Else Messagebox("Failed","Return Value: "+String(ll_RowCount)) End If Else //Checks if any warning If len(ls_Error) > 0 Then MessageBox("Warning", "With warning information:~r~n" + ls_Error) Else MessageBox("Succeed", "Return Value: " + String(ll_RowCount) ) End If End If |
This example imports data (and state) from a standard-format JSON
string in rows 1 through 10 into the primary buffer of the DataWindow (see
an example of a standard-format JSON
string):
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
long ll_RowCount string ls_StandardJson, ls_Error //Standard JSON ls_StandardJson = … ll_RowCount = dw_1.ImportJson(ls_StandardJson, ls_Error, Primary!, 1, 10) //Checks if any error IF isnull(ll_RowCount) Then Messagebox("Error", "The method returns null") ElseIf ll_RowCount < 0 Then If len(ls_Error) > 0 Then Messagebox("Failed","Return Value: " + String(ll_RowCount) & + "~r~nWith error information:~r~n" + ls_Error) Else Messagebox("Failed","Return Value: "+String(ll_RowCount)) End If Else //Checks if any warning If len(ls_Error) > 0 Then MessageBox("Warning", "With warning information:~r~n" + ls_Error) Else MessageBox("Succeed", "Return Value: " + String(ll_RowCount) ) End If End If |
This example imports data (and state) from the standard-format JSON
string in rows 1 through 10 and in columns 2 through the end into the
primary buffer of the DataWindow (see an example of a standard-format JSON
string):
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
long ll_RowCount string ls_StandardJson, ls_Error //Standard JSON ls_StandardJson = … ll_RowCount = dw_1.ImportJson(ls_StandardJson, ls_Error, Primary!, 1, 10, 2) //Checks if any error IF isnull(ll_RowCount) Then Messagebox("Error", "The method returns null") ElseIf ll_RowCount < 0 Then If len(ls_Error) > 0 Then Messagebox("Failed","Return Value: " + String(ll_RowCount) & + "~r~nWith error information:~r~n" + ls_Error) Else Messagebox("Failed","Return Value: "+String(ll_RowCount)) End If Else //Checks if any warning If len(ls_Error) > 0 Then MessageBox("Warning", "With warning information:~r~n" + ls_Error) Else MessageBox("Succeed", "Return Value: " + String(ll_RowCount) ) End If End If |
This example imports data (and state) from a standard-format JSON
string in rows 1 through 10 and in columns 2 through 5 into the primary
buffer of the DataWindow (see an
example of a standard-format JSON string):
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
long ll_RowCount string ls_StandardJson, ls_Error //Standard JSON ls_StandardJson = … ll_RowCount = dw_1.ImportJson(ls_StandardJson, ls_Error, Primary!, 1, 10, 2, 5) //Checks if any error IF isnull(ll_RowCount) Then Messagebox("Error", "The method returns null") ElseIf ll_RowCount < 0 Then If len(ls_Error) > 0 Then Messagebox("Failed","Return Value: " + String(ll_RowCount) & + "~r~nWith error information:~r~n" + ls_Error) Else Messagebox("Failed","Return Value: "+String(ll_RowCount)) End If Else //Checks if any warning If len(ls_Error) > 0 Then MessageBox("Warning", "With warning information:~r~n" + ls_Error) Else MessageBox("Succeed", "Return Value: " + String(ll_RowCount) ) End If End If |
This example imports data (and state) from a standard-format JSON
string in rows 1 through 10 and in columns 2 through 5 into the primary
buffer of the DataWindow beginning in column 2 (see an example of a standard-format JSON
string):
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
long ll_RowCount string ls_StandardJson, ls_Error //Standard JSON ls_StandardJson = … ll_RowCount = dw_1.ImportJson(ls_StandardJson, ls_Error, Primary!, 1, 10, 2, 5, 2) //Checks if any error IF isnull(ll_RowCount) Then Messagebox("Error", "The method returns null") ElseIf ll_RowCount < 0 Then If len(ls_Error) > 0 Then Messagebox("Failed","Return Value: " + String(ll_RowCount) & + "~r~nWith error information:~r~n" + ls_Error) Else Messagebox("Failed","Return Value: "+String(ll_RowCount)) End If Else //Checks if any warning If len(ls_Error) > 0 Then MessageBox("Warning", "With warning information:~r~n" + ls_Error) Else MessageBox("Succeed", "Return Value: " + String(ll_RowCount) ) End If End If |
See also