json

Top  Previous  Next

json

jsonobj=new("json")

 

The json object provides functionality to convert data from different file structures often found in UnForm server operations into JSON format used often in Javascript code.  The common use for this function is for custom web form development, where data from a server-run rule set can be obtained in Javascript at runtime using the runRuleSet function found in common.js and available in the browser interface to document management archives.

 

All text is assumed to be encoded as ISO-8859-1.

 

Methods

fromdlm$(fileorstring$ [,delim$ [,quotes [,header$]]]) returns a JSON representation of a delimited file, such as a CSV file or tab-delimited file.  If no delim$ character is provided, a tab ($09$) character is assumed.  Quotes can be 0 or 1; 1 indicates that data that might contain the delimiter will be quoted.  The first line of the file is assumed to contain column header names, and these names are used as the JSON object names (after replacing invalid name characters with underscores).  Some delimited files do not contain a header row, in which case you can supply a delimited list of column names in header$.

 

The response is an array of objects, where each file row is an array element, and each row is represented as an object with name:value pairs.

 

If the file doesn't exist, the value of the argument is used as if it was file content.

fromcsv$(fileorstring$[,header$]) is equivalent to fromdlm$(fileorstring$,",",1,header$)

fromini$(fileorstring$) parses a file or string in INI format, with section headers in square brackets (i.e. [main]) and section data made up of lines of name=value pairs.  Each section becomes an object whose value is another object.

fromtpl$(template$) returns a JSON object representing fields and data in a string template.  For example, a library object can obtain properties of a document in a template: obj'getdoc(doctype$,docid$,tpl$), and the returned tpl$ value can be converted to JSON format using this method.  Numeric and string types are maintained during this conversion.

jsencode$(str$) returns a string encoded to be safe to include in a quoted JSON string object, by backslash-escaping backslashes, quotes, linefeeds, and other binary and control characters.

totpl(json$,tpl$) parses json$ string and return a string template in tpl$ with field names matching the name elements in the json structure.  Arrays are converted to name.n notation.  All template variables are strings, so must be accessed as tpl.name$.  Some complex or nested json structures are not convertible.  Returns 1 on success, 0 if it fails, in which case tpl$ will be null.

 

A CSV to JSON example:

 

"First Name","Last Name",Age

"Joe","Smith",40

"Sue","Smothers",45

 

[ {First_Name:"Joe", Last_Name:"Smith", age:40} ,

 {First_Name:"Sue", Last_Name:"Smothers", age:45

]

 

An INI to JSON example:

 

[Section 1]

Name1=Value 1

Name2=Value 2

[Section 2]

Name1=Value 1

Name2=Value 2

 

{

 Section_1: {Name1="Value 1", Name2="Value 2" },

 Section_2: {Name1="Value 1", Name2="Value 2"}

}