ImportJsonByKey

Description

Inserts data from a JSON string into a DataWindow control, DataStore object, or DataWindowChild object according to the key name of the JSON item.

The JSON string must be in the format described in the section called “Supported JSON formats” in Application Techniques.

This 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 DataStore object, except for those with the Composite, Crosstab, OLE 2.0, or RichText presentation styles.


Syntax

PowerBuilder

long dwcontrol.ImportJsonByKey( string json {, string error} {, DWBuffer dwbuffer {, long startrow {, long endrow } } } )

Argument

Description

dwcontrol

A reference to a DataWindow control, DataStore, or DataWindowChild.

json

A string specifying the JSON data. The JSON string must comply with the the section called “Supported JSON formats” in Application Techniques. If it is DataWindow JSON, this function will ignore the mapping-method element of the JSON data.

error (optional)

A variable into which the returned warning or error message will be placed.

When there are a large amount of error messages, the error information will only display the total number of errors, and the detailed message of the first 10 errors.

The import warning caused by data type mismatch will not affect the return value of ImportJsonByKey; although the data of the mismatched columns will not be imported, the rest columns (even only one column) that are matched will be imported successfully; and that row will be regarded as a successful import and counted into the return value.

The import error caused by DW presentation style mismatch, invalid arguments, startrow value greater than the number of rows,etc. will be regarded as a failure, and represented by a negative return value of ImportJsonByKey, instead of being placed into this variable. See the Return Value section for more.

Most of the messages placed into this variable are warnings (such as data type mismatch) rather than errors. Developers can adjust the JSON data according to the message or simply ignore the message if the problematic column is not critical and the corresponding DataWindow column can be left blank.

dwbuffer (optional)

A value of the dwBuffer enumerated datatype identifying the DataWindow buffer from which you want to import the data. For a list of valid values, see DWBuffer.

For plain JSON: If not specified, imports the JSON data to the Primary! buffer. If specified, imports the JSON data to the specified buffer.

For DataWindow JSON: If not specified, imports data of all of the buffers from the JSON string to the corresponding buffers and, if any, imports the data for DataWindowChild. If specified, imports data of the specified buffer from the JSON string to the corresponding buffer.

startrow (optional)

The number of the first detail object in the JSON Array that you want to import. The default is 1. If it is 0 or negative, 1 is used.

endrow (optional)

The number of the last detail object in the JSON Array that you want to import. The default is the rest of the objects. If it is 0 or negative, it indicates the rest of rows.


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, or no JSON key matches with the DataWindow column.

-1 -- General error.

-2 -- No rows or startrow value supplied is greater than the number of rows in the JSON.

-3 -- Invalid argument.

-4 -- Invalid JSON.

-5 -- JSON format error.

-6 -- Unsupported DataWindow presentation style for import.

-7 -- Error resolving DataWindow nesting.

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 imports data from a plain JSON string into the corresponding column of DataWindowChild:

long ll_RowCount
string ls_PlainJson
datawindowchild ldwc_Dept

dw_1.GetChild("dept_id", ldwc_Dept)

ls_PlainJson = '[{"dept_id":100,"dept_name":"R & D"},{"dept_id":200,"dept_name":"Sales"}, &
                  {"dept_id":300,"dept_name":"Finance"},{"dept_id":400,"dept_name":"Marketing"}]' 
ll_RowCount = ldwc_Dept.ImportJsonByKey(ls_PlainJson)

This example imports data from a plain JSON string into the corresponding column of DataWindowChild:

long ll_RowCount
string ls_PlainJson, ls_Error
datawindowchild ldwc_Dept

dw_1.GetChild("dept_id", ldwc_Dept)

ls_PlainJson = '[{"dept_id":100,"dept_name":"R & D"},{"dept_id":200,"dept_name":"Sales"}, &
                  {"dept_id":300,"dept_name":"Finance"},{"dept_id":400,"dept_name":"Marketing"}]'
ll_RowCount = ldwc_Dept.ImportJsonByKey(ls_PlainJson , 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 from a plain JSON string into the primary buffer of the DataWindow:

long ll_RowCount
string ls_PlainJson

ls_PlainJson = '[{"dept_id":100,"dept_name":"R & D","dept_head_id":501}, &
                  {"dept_id":200,"dept_name":"Sales","dept_head_id":902}]'
ll_RowCount = dw_Dept.ImportJsonByKey(ls_PlainJson, Primary!)

This example imports data from dw_1 into a DataWindow JSON string, and then imports data in rows 2 through the end from the JSON string into dw_2 (suppose dw_1 and dw_2 have the same column names):

long ll_RowCount
string ls_DWJson

ls_DWJson = dw_1.ExportJson(true)
ll_RowCount = dw_2.ImportJsonByKey(ls_DWJson, Primary!, 2)

This example imports data (and state) in rows 1 through 10 from the primary buffer of a DataWindow JSON string into the primary buffer of the DataWindow:

long ll_RowCount
string ls_DWJson

//DataWindow JSON
ls_DWJson = …

ll_RowCount = dw_1.ImportJsonByKey(ls_DWJson, Primary!, 1, 10)

This example imports data from a plain JSON string into the primary buffer of the DataWindow:

long ll_RowCount
string ls_PlainJson, ls_Error

ls_PlainJson = '[{"dept_id":100,"dept_name":"R & D","dept_head_id":501}, &
                  {"dept_id":200,"dept_name":"Sales","dept_head_id":902}]'
ll_RowCount = dw_Dept.ImportJsonByKey(ls_PlainJson, 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 dw_1 into a DataWindow JSON string, and then imports data in rows 2 through the end from the JSON string into dw_2 (suppose dw_1 and dw_2 have the same column names):

long ll_RowCount
string ls_DWJson, ls_Error

ls_DWJson = dw_1.ExportJson(true)

ll_RowCount = dw_2.ImportJsonByKey(ls_DWJson, ls_Error, Primary!, 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) in rows 1 through 10 from the primary buffer of a DataWindow JSON string into the primary buffer of the DataWindow:

long ll_RowCount
string ls_DWJson, ls_Error

//DataWindow JSON
ls_DWJson =…

ll_RowCount = dw_1.ImportJsonByKey(ls_DWJson, 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

See also

ImportJson

ExportJson