importjson
JSON
,
OPTION
,
VALUE
The importjson statement imports a JSON array into the current database.
Parameters
This statement has three parameters:
json – JSON record to import.
option – An option for customizing how JSON data is imported. The available options include existingdata, addfields, missingfieldserror and mapfields. See below for descriptions of each these options. The option name can be specified in either upper or lower case.
value – The new value for this option.
Description
This statement imports a JSON array into the current database. The JSON array must contain one or more JSON records, each of which will be imported into an individual Panorama record. (JSON is a data interchange format that is used by many web APIs. See http://www.json.org for more information.) The imported records are normally appended to the end of the database, but you can also replace the current database contents with the JSON data.
Here is some typical JSON data. In this example there are four records. Notice that different records contain different items – that is perfectly fine (you’ll also notice that the order of the items within each record is unimportant).
[
{ "Name" : "Bob Smith", "Email" : "bsmith@mail.com", "City" : "Tustin" },
{ "Name" : "Margo Wilson", "Email" : "mw@zmail.com", "City" : "Placentia" },
{ "Name" : "Ted Jarvis", "Phone" : "(714) 598-2847", "Wage" : 12.50 },
{ "Phone" : "(714) 348-9033", "Name" : "Bill Jackman", "Email" : "wjack@zmail.com", "City" : "Brea" }
]
Suppose the text above was in a file in your Documents folder named Contacts.json. You can create a new Panorama database containing this data with this two line procedure:
newdatabase
importjson fileload("~/Documents/Contacts.json")
The first line, newdatabase
, creates an untitled database with one field (A) and one record. The second line, importjson
, imports the four records into the new database. In the process, it creates 4 text fields (Name, Email, City and Phone) and one floating point field (Wage). Note: Since the original database was empty, the original field (A) is removed.
Now suppose you have a second file named Contacts2.json.
[
{ "Name" : "Jordan Kelso", "Email" : "jkelso@mail.com", "Wage" : 11.75, "Phone" : "(562) 479-3920 },
{ "Name" : "Milo Sparks", "Phone" : "(714) 490-3920", "Wage" : 14.50, "Organization" : "Sparks Cleaning" },
]
You can add this data to the new database with another importjson
statement.
importjson fileload("~/Documents/Contacts2.json")
This statement will add two more records to the database, and one more field (Organization).
Importing Dates
When data is imported into a pre-existing date field, Panorama will process the data just as it would data entry into the field, so it will accept data like 6/19/17
, Jan 5, 2001
, or even today
or next tuesday
. So if the database already contains a field named Hired, this example will import February 4, 2007 into that field.
importjson ||| [
{ ... },
{ "Last" : "Wilson", ... , "Hired" : "2/4/07" },
{ ... }
] |||
What if the Hired field doesn’t already exist? In that case, Panorama has no absolute way to make sure that information is a date (perhaps 2/4/07
is a part number). Because it can’t be certain, the importjson statement will create Hired as a text field. You can manually change this into a date field later.
There is one case where Panorama will create a date field automatically. Many JSON applications (including Panorama itself) encode dates using the format:
YYYY-MM-DDTHH-MM-SSZ
If the importjsonline statement encounters a value in this format, it will automatically create a date field instead of a text field. (However, it will only do this if the time is midnight, as shown in this example).
importjson ||| [
{ ... },
{ "Last" : "Wilson", ... , "Hired" : "2007-04-27T00-00-00Z" },
{ ... }
] |||
The example above will automatically create a date field named Hired if it doesn’t already exist.
IMPORT OPTIONS
In addition to the text to import, you can also specify multiple option/value pairs in a single importjson statement, like this:
importjson text,option1,value1,option2,value2,option3,value3
The possible options are existingdata, addfields, missingfieldserror and mapfields. Each of these options is described in detail below.
EXISTINGDATA
The importjson statement normally appends the imported data to the end of the current database. You can use this option to tell it to replace the data instead.
importjson Presidents,"ExistingData","Replace"
Be careful! This will erase all of the existing data in the database! (Note: The replace option is not available for shared multi-user databases, attempting to use this option will cause an error.)
If you want to append the imported data, use append instead of replace (or just leave this option off).
ADDFIELDS
The importjson statement normally adds new fields as necessary to accomodate all the data in the JSON text. However, you can use this option to disable that.
importjson jsontext,"addfields","no"
Note: If you are importing into a shared multi-user database (connected to a server), you must disable this option, since fields can’t be added to a shared database (except through the “new generation” process.)
MISSINGFIELDSERROR
If the JSON text contains items that don’t have corresponding database fields, the importjson statement will stop the procedure with an error. If you want to ignore the error you can use if error
, or you can use the missingfieldserror option, like this:
importjson jsontext,"addfields","no","missingfieldserror","no"
MAPFIELDS
The importjson statement normally assumes that items in the JSON record have the same name as database fields. For example, it will place the JSON Product item into the database Product field, and the Quantity item into the Quantity field. But what if the names don’t match? Perhaps your database fields are named SKU and Qty instead of Product and Quantity. The mapfields option allows you to specify which JSON fields match which database fields. The mapfields value must contain a line for each field. The line starts with the JSON item name, followed by the → character, followed by the database field name. (You can copy the → character right from this page.)
JSON Item 1→Database Field 1
JSON Item 2→Database Field 2
JSON Item 3→Database Field 3
In this example, the Panorama database contains fields named SKU and Qty. The JSON items Item and Quantity will be copied into these fields. (Note: If the fields don’t exist, they will be created automatically.)
local jsontext
jsontext = ||| [
{ "Product" : "Left Handed Bob Wrench", "Quantity" : 25 },
{ "Product" : "Right Handed Bob Wrench", "Quantity" : 85 },
{ "Product" : "Thor Hammer", "Quantity" : 15 }
] |||
importjson jsontext,"mapfields",commatocr("Product→SKU,Quantity→Qty")
The mapfields option can also be used to ignore certain JSON items. To do that, simply map the database field name to empty. In this example, the JSON Vendor item will be ignored.
local jsontext
jsontext = ||| [
{ "Product" : "Left Handed Bob Wrench", "Quantity" : 25, "Vendor" : "Giant Conglomerate" },
{ "Product" : "Right Handed Bob Wrench", "Quantity" : 85, "Vendor" : "Giant Conglomerate" },
{ "Product" : "Thor Hammer", "Quantity" : 15, "Vendor" : "Norwegian Tools" }
] |||
importjson jsontext,"mapfields",commatocr("Product →SKU,Quantity→Qty,Vendor→")
Complex JSON Items
In all the examples shown so far the JSON values have been simple fields or numbers. These simple values are straightforward to import into Panorama fields. But JSON values can also be complex values built up of multiple items. If this type of JSON value is encountered, the importjson statement will encode the data in a binary field using either a Panorama dictionary or data array.
Consider this JSON record, which contains a subarray for children:
{
"Name" : "Melanie Billings",
"Spouse" : "Robert",
"Children" : [ "Mark", "Naomi", "Victoria" ]
}
If imported into a Panorama database, the data will flow into three fields: Name, Spouse and Children. The Name and Spouse fields are ordinary text fields, but Children will be a binary field that contains a data array. Different children can be retrieved with the array( function.
array(Children,1) ☞ Mark
array(Children,2) ☞ Naomi
array(Children,3) ☞ Victoria
After importing, you could create a new text field called ChildNames, and then transfer the names into this field in a comma separated format using the exportdataarray( function:
field ChildNames
formulafill exportdataarray(Children,",")
The previous example included an array, now let’s look at a JSON record that contains a subrecord for phone numbers.
{
"Name" : "Tonya Adams",
"Title" : "Editor",
"Phone" :
{
"Work" : "(847) 442-9283",
"Home" : "(847) 309-2284",
"Cell" : "(903) 662-3022"
}
}
The phone number information will flow into a Panorama binary field named Phone. Different phone numbers can be extracted with the getdictionaryvalue( function.
getdictionaryvalue(Phone,"Cell") ☞ (903) 662-3022
getdictionaryvalue(Phone,"Home") ☞ (847) 309-2284
getdictionaryvalue(Phone,"Work") ☞ (847) 442-9283
Use the listdictionarykeys( function to get a list of all of the available phone numbers.
See Also
- @import --
- @json --
- Importing a Panorama 6 Database -- Importing a Panorama 6 Database
- addfield -- adds a new field to the current database (on the end).
- addlines -- adds a specified number of records to the end of a database.
- addrecord -- adds a new record at the end of the current database.
- arraybuild -- builds an array by scanning a database and creating an array element for every record (including invisible records) in the database (see Text Arrays).
- arraybuild( -- builds an array by scanning a database and creating an array element for every record (including invisible records) in the database (see Text Arrays).
- arrayselectedbuild -- builds an array by scanning a database and creating an array element for every visible (selected) record in the database (see Text Arrays).
- arrayselectedbuild( -- builds an array by scanning a database and creating an array element for every visible (selected) record in the database (see Text Arrays).
- autoallfieldwidths -- automatically sets the width of all fields based on the data in each field.
- autofieldwidth -- automatically sets the width of the current field based on the data in it.
- automaticfieldchoices -- updates the current field's Choice list with actual data in the database.
- Auxiliary Databases -- automatically opening multiple databases associated with each other.
- cell -- enters a value into the currently active field (i.e. cell).
- checkdesignlock -- checks if field structure can be changed, if not, returns an error.
- clearcell -- deletes the contents of the current field.
- clearrecord -- deletes the currently selected record.
- closedatabase -- closes a specified database.
- closefile -- closes the current database.
- commonfieldspopup -- pops up a list of common fields, and changes the current field specifications when a field is chosen from this menu.
- constructfields -- creates one or more new fields based on a template.
- copycell -- copies the contents of the current field onto the clipboard,
replacing anything previously on the clipboard.
- copyrecord -- copies the contents of the current record onto the clipboard.
- Creating a New Database -- creating a new database.
- Custom Database Initialization -- using an .Initialize procedure to automatically run custom code when a database opens.
- cutcell -- delete the contents of the current cell and places the deleted data on the clipboard.
- cutline -- copies the current record to the clipboard and deletes it.
- cutrecord -- copies the current record to the clipboard and deletes it.
- Data Input Validation -- validation of data entry (checking for duplicates, etc.)
- Database Integrity Checks -- checking database integrity when opening and saving.
- Database is Damaged Alert -- how to fix the "database is damaged" alert when opening a database.
- databaseconsoledump -- dumps the raw contents of the specified database to the console in comma delimited format.
- databaseexportcsv -- exports selected records in the current database in CSV format (comma separated) to a text file.
- databaseexportjson -- exports selected records in the current database in JSON format to a text file.
- databaseexporttsv -- exports selected records in the current database in TSV format (tab separated) to a text file.
- databaseexportusingformula -- exports selected records in the current database to a text file, using a formula.
- datatype( -- determines what kind of data is in a field or variable: text, number, etc.
- datavalidationalert -- displays an alert when invalid data has been input.
- datavalue( -- returns the value of a field or variable: text, number, etc.
- dbcheckopen( -- returns true if the specified database is currently open, false if it is not.
- dbinfo( -- gets information about a database: what forms it contains, what fields, what flash art pictures, etc.
- deleteabove -- copies the current record to the clipboard and deletes it, then moves to the record above.
- deletefield -- deletes the current field from the database.
- deleterecord -- deletes the currently selected record.
- downloadpartialdatabase -- downloads specified components of the current database from the server. The current database must be shared.
- downrecord -- moves the cursor down one visible record.
- duplicaterecord -- duplicates the current record.
- editcell -- opens the edit window for the currently active field (or cell), highlighting the data, and allows you to edit that field.
- editcellstop -- opens the edit window for the currently active field (or cell), highlighting the data, and allows you to edit that field.
- editcellwith -- opens the edit window for the currently active field (or cell), using the supplied alternate text.
- editfield -- begins editing of the specified field.
- emptydatabase( -- returns true if the current database is completely blank.
- emptyfield -- tells Panorama to move to the next available (empty) field in a series of line item fields.
- emptyline( -- returns true if the entire current record (all fields) is blank, otherwise it is false.
- excludefromrecentmenu -- excludes the next opened database from the Recent menu.
- export -- exports the selected rows in the current database into a text file.
- exportjson -- exports the current database in JSON format.
- fieldname -- changes the name of the current field.
- fieldtype -- changes the data type of the current field.
- fieldvalue( -- grabs the contents of a field in the current record of a database. You can grab data from the current database, or from another database.
- firstrecord -- makes the first visible (selected) record in the database the active record.
- formulacalc -- allows you to evaluate a formula that you were not able to
code into the procedure when it was being written.
- formulastrings( -- returns a data array of strings (text constants) used in a formula.
- formulavalue -- calculates the result of a formula. Usually this is done with an assignment statement (for example `x=2 * y` ), but the *formulavalue* statement gives you more flexibility. You can specify what database is to be used for the calculation (an assignment statement always uses the current database) and you can specify the formula using a variable, making it easy to change on the fly. This statement also gives you more control over how errors are handled.
- formulavalue( -- calculates the result of a formula based on data in the current record of any open database. This is similar to the fieldvalue( function, but allows any formula to be used, rather than just a single field.
- getautonumber -- returns the automatically generated number for the next record that will be added to the database.
- getautonumber( -- returns the automatically generated number for the next record that will be added to the database.
- getfieldproperties( -- returns a dictionary containing all of the properties of the specified field. (See the setfieldproperties statement if you want to change one or more field properties.)
- getproceduretext -- gets the contents (source) of a procedure and places it in a variable.
- growl -- displays a notification.
- growlmessage -- displays a notification.
- importdatabase -- imports data from another database into the current database.
- Importing Binary Data from Panorama 6 -- Importing Binary Data from Panorama 6
- importjsonline -- imports a JSON record into the current record.
- importline -- imports a line of text into the current record.
- importrawdata -- imports raw binary data into an existing database.
- importtext -- imports text into an existing database.
- info("databasefilename") -- returns the name of the current database.
- info("rawdatabasedata") -- returns the binary data of the current database.
- info("rawrecord") -- returns the binary data of the current record.
- info("rawrecordid") -- returns a number that represents the location of the current record within the database.
- insertbelow -- inserts a new record below the current record.
- insertbelowcarriagereturn -- inserts a new record below the current record and moves to the first column.
- insertfield -- inserts a new field into the database in front of the current field.
- insertrecord -- inserts a new record above the current record.
- join -- joins data from another database into the current database.
- joinonerecord -- joins matching data from another database into the current record.
- lastlookupdatabase( -- returns the name of the database referenced in the most recent lookup.
- lastrecord -- makes the last visible (selected) record in the database the active record.
- left -- makes the previous field active in the active window.
- listchoices( -- builds a text array containing a list of all the values stored in a specified field.
- lookup( -- searches a database for a value, then returns other information from the same record. For example, the *lookup(* function can look up a phone number given a customer name, or look up a price given a part number.
- lookupall( -- builds a text array containing one item for every record in the target database where the data in the keyField matches the keyData. Each item in the text array contains the value extracted from the dataField for that record. If the data field is a numeric or date field, it is converted to text using the default patterns for that field.
- lookupalldouble( -- is similar to the lookupall( function, but it returns two fields from the target database instead of just one.
- lookupalloctet( -- is similar to the lookupall( function, but it returns eight fields from the target database instead of just one.
- lookupallquadruple( -- is similar to the lookupall( function, but it returns four fields from the target database instead of just one.
- lookupallquintuplet( -- is similar to the lookupall( function, but it returns five fields from the target database instead of just one.
- lookupallseptuplet( -- is similar to the lookupall( function, but it returns seven fields from the target database instead of just one.
- lookupallsextet( -- is similar to the lookupall( function, but it returns six fields from the target database instead of just one.
- lookupalltriple( -- is similar to the lookupall( function, but it returns three fields from the target database instead of just one.
- lookuplast( -- searches the selected records in a database for a value, then returns other information from the same record. For example, the *lookuplast(* function can look up a phone number given a customer name, or look up a price given a part number. Unlike the lookup( function which searches from the top of the database, the *lookuplast(* function searches backwards from the bottom.
- lookuplastselected( -- searches the selected records in a database for a value, then returns other information from the same record. For example, the *lookuplastselected(* function can look up a phone number given a customer name, or look up a price given a part number. Unlike the lookupselected( function which searches from the top of the database, the *lookuplastselected(* function searches backwards from the bottom.
- lookupmoredata( -- looks up an additional data field based on the previous lookup.
- lookupmoreformula( -- looks up additional data based on a previous lookup.
- lookupselected( -- searches the selected records in a database for a value, then returns other information from the same record. For example, the *lookupselected(* function can look up a phone number given a customer name, or look up a price given a part number.
- movefieldbefore -- moves the current field to a new position.
- moverecorddown -- moves the current record down one line.
- moverecordup -- moves the current record up one line.
- newdatabase -- creates a new database.
- newdatabasewithfields -- creates a new database with one or more fields.
- newdatabasewithjson -- creates a new database using a JSON file.
- newdatabasewithtemplate -- creates a new database with a template.
- newdatabasewithtextfieldnames -- creates a new database from a text file. The text file must contain the field names in the first line.
- newdatabasewithtextfile -- creates a new database from a plain text file. The fields will be named A, B, C, etc.
- opendatabase -- opens a database file.
- openfile -- opens a database file. It can also import data from a text file, another database, or a variable into the current database.
- Opening a Database in Diagnostic Mode -- opening a database so that no implicit code is triggered.
- openplain -- opens a database without opening any pre-saved windows, just the data sheet.
- opensecret -- opens a database invisibly, without opening its windows.
- opentextfile -- imports data from a text file or a variable into the current database.
- panoramastartupdatabases -- opens any databases specified for startup.
- partialdatabaseupdate -- updates one or more components of an existing database.
- pastecell -- pastes the contents of the clipboard into the current cell.
- pasteline -- inserts a new record containing the contents of the clipboard.
- pasterecord -- inserts a new record containing the contents of the clipboard.
- printpdf -- prints the current database to a PDF file.
- printpreview -- previews what printing the current window will look like.
- printpreviewonerecord -- previews what printing the current record will look like.
- recalculatefield -- recalculates all values in current field based on field's formula (selected records only).
- recompile -- recompiles all procedures in a database.
- reorderfieldsdialog -- opens the Reorder Fields dialog.
- right -- moves the cursor to the next field in the active window.
- runfieldcalculations -- performs any automatic calculations associated with the current field.
- runfieldcode -- runs the automatic code associated with the specified field.
- runfieldsideeffects -- performs any side effects associated with the current field (calculations, procedures, etc.).
- saveandclose -- saves and closes a database file.
- saveblueprint -- save a blueprint of a database's structure.
- savepartialdatabase -- saves specified components of the current database, leaving other components out.
- serverdatabaseconsoledump -- dumps the raw contents of the specified database on the server to the console in comma delimited format.
- serverlookup( -- searches a shared database for a value, then returns other information from the same record. Similar to the lookup( function, but the search is performed on the server instead in the local computer's memory.
- setactivedatabase -- makes a database active (without changing the configuration of the windows).
- setautonumber -- changes the automatically generated number for the next record that will be added to database. This allows you to generate numbers out of sequence, or to start the sequence at a specific value.
- setdatabaseoptions -- modifies one or more properties of a database.
- setfieldnames -- changes the names of all database fields at once.
- setfieldproperties -- modifies one or more properties (name, data type, formula, etc.) of the current field.
- sizeof( -- calculates the amount of memory used by a field or a variable.
- superarraybuild -- scans a database to create a text array. This statement is is similar to arraybuild, but with a number of additional options (see Text Arrays).
- superlookup( -- searches a database for a record that matches a query formula, then uses a second formula to return other information from the same record. This is similar to the lookup( function, but instead of using an individual field for the key and data, any valid true/false (Boolean) formula can be used.
- table( -- searches a database for a value, then returns other information from the same record. Unlike the lookup( function, the *table(* function does not require an exact match. If it does not find an exact match the *table(* function will use the closest match. For example, the *table(* function can look up a tax rate given an income amount, or look up a shipping price given a zip code and weight.
- uniqueid( -- generates ID codes with a text root and a numeric suffix (for example `Jeff261`).
- unlockrecord -- unlocks the currently active record.
- uploadrecord -- uploads the current record to the server, even if it was not locked (for emergency use only).
- uprecord -- moves the cursor up one visible record.
History
10.2 | Update | If the database is shared (multi-user), you cannot use the ADDFIELDS or REPLACE option with this statement. |
10.0 | New | New in this version. |