json — n_pbt_json #
← Component reference · Guide contents
Build a JSON type by type — string, number, boolean, array, nested object — with the escaping done for you, then read one back along a path:
of_load,of_get_string,of_type,of_count. A single object builds your requests and reads your responses. All in pure PowerScript: no page, no engine, PowerBuilder 10 included.
▶ See it live — Demo application, JSON tile: the results, the code behind them and this page, side by side.
At a glance #
n_pbt_json is autoinstantiate: declare it as a local variable, no create or destroy. It is the very object that internally builds the payloads sent to the engine — now offered to your code, on the build and the read side.
Quick start #
Build a payload, then read a response back — it all fits in one local variable:
// Local variables
n_pbt_json lnv_json
string ls_customer
double ld_total
// Write : each member is typed and escaped for you - a path may have levels
lnv_json.of_set_string(/*path*/ "customer", /*value*/ "Ada")
lnv_json.of_set_number(/*path*/ "total", /*value*/ 690.50)
lnv_json.of_set_boolean(/*path*/ "paid", /*value*/ true)
// lnv_json.of_text() -> {"customer":"Ada","total":690.5,"paid":true}
// Read : load a response, reach values by path
lnv_json.of_load(/*json*/ '{"order":{"customer":"Ada","total":690.50}}')
IF lnv_json.of_is_valid() THEN
ls_customer = lnv_json.of_get_string(/*path*/ "order/customer") // Ada
ld_total = lnv_json.of_get_number(/*path*/ "order/total") // 690.5
END IF
A path is the keys from the root joined by /; an array index is its position, counted from 1, like every position of the library: order/lines/1 is the first line, order/lines/0 addresses nothing. An empty segment (a//b, /a) or an index 0 on an array addresses nothing — a write refuses it with -5 — and a key that holds a / cannot be addressed. When an object writes the same key twice, its FIRST occurrence is the one read and written — a browser keeps the last one.
Build a payload #
| Method | Role |
|---|---|
of_add_string (string as_path, string as_value) → long | Appends a STRING, escaped for you, to the ARRAY at as_path. The array must already be there: open it first with of_set_raw and an empty array. Returns 0, -4 when as_path is missing or is not an array, -5 when a segment of as_path is empty or an index 0 on an array; a null string is written empty (for a JSON null: of_add_raw(as_path, "null")) |
of_add_boolean (string as_path, boolean ab_value) → long | Appends a BOOLEAN (true or false, never quoted) to the ARRAY at as_path. The array must already be there: open it first with of_set_raw and an empty array. Returns 0, -4 when as_path is missing or is not an array, -5 when a segment of as_path is empty or an index 0 on an array; a null value writes null |
of_add_number (string as_path, long al_value) → long | Appends a NUMBER (five overloads: integer, long, longlong, decimal, double — a decimal and a longlong keep EVERY digit (the cents of an amount, the last digit of an id above 2^53), and a literal such as 690.50 is a decimal, written 690.5) to the ARRAY at as_path. The array must already be there: open it first with of_set_raw and an empty array. Returns 0, -4 when as_path is missing or is not an array, -5 when a segment of as_path is empty or an index 0 on an array; a null value writes null |
of_add_raw (string as_path, string as_json) → long | Appends a value ALREADY in JSON (an object, an array) you vouch for to the ARRAY at as_path. The array must already be there: open it first with of_set_raw and an empty array. Returns 0, -4 when as_path is missing or is not an array, -5 when a segment of as_path is empty or an index 0 on an array; a null or empty one writes null. The empty path is the ROOT: of_load("[]") then of_add_raw("", …) builds a document that is an array (the body of a bulk API call). Adding many elements in a row to the same array stays quick |
of_set_array (string as_path, string as_values[]) → long | WRITES a whole ARRAY of strings at as_path from a PowerBuilder array, each element escaped. An empty array writes an empty one, a null element an empty string. Same path rule and same codes as of_set_string |
of_set_members (string as_fragment) → long | Merges members already formed elsewhere at the ROOT, comma-joined. An empty fragment writes nothing. Returns 0, -4 when the root of the document is not an object |
of_clear ( ) → long | Empties the document to start another object with the same variable. Returns 0 |
of_text ( ) → string | The document as it stands (compact): what you built (of_add_string, of_add_number...) or loaded (of_load), after any change — the single output, ready to send, store or save back. of_pretty is the indented form. {} when empty; calling it empties nothing |
Read a document #
| Method | Role |
|---|---|
of_load (string as_json) → long | Loads a JSON text for reading. Returns 0 when well-formed, -5 when the argument is empty or blank (spaces, tabs, line breaks), -4 when it is not valid JSON or nests more than 512 levels (the document is then dropped). Parses in pure PowerScript. A byte order mark (U+FEFF) in front, the one of a file saved as UTF-8 with BOM, is skipped |
of_is_valid ( ) → boolean | True when the object holds a document: the last of_load succeeded, or something was written since. A value given to of_set_raw / of_add_raw is not re-checked |
of_exists (string as_path) → boolean | True when a value lives at the given path — tells a present-but-empty value from an absent one |
of_type (string as_path) → string | The JSON type at the path: object, array, string, number, boolean, null; empty if absent |
of_get_string (string as_path) → string | The value at the path, as text: a string comes back decoded, a number/boolean/null as text, an object or array as raw JSON (reloadable by of_load); a key written twice is read at its FIRST occurrence |
of_get_number (string as_path) → double | The number at the path, as a double — the decimal dot is honoured on any machine (2.5 is 2.5, not 2). 0 if absent; the exponent is resolved (1e-7, 2.5E+3) and 17 significant digits are kept, leading zeros of the exponent included. For an amount or an id that must keep every digit: of_get_decimal or of_get_longlong |
of_get_decimal (string as_path) → decimal | The number at the path, as a decimal, rebuilt from the TEXT of the number and never through a double: 12345678901234.56 comes back with its cents, 2.5E-3 is 0.0025. 28 significant digits at most. 0 if absent, when the value is not a number, or beyond what a decimal holds |
of_get_longlong (string as_path) → longlong | The number at the path, as a 64-bit integer, rebuilt from its text and never through a double: a database id such as 9007199254740993 comes back whole. Decimals are cut off (2.9 gives 2). 0 if absent, when the value is not a number, or beyond the range of a longlong |
of_get_boolean (string as_path) → boolean | The boolean at the path: true only for the JSON literal true |
of_count (string as_path) → long | The number of members of the object, or elements of the array, at the path. Returns -1 when the value is scalar or absent (tells an empty array, 0, from a missing one, -1). With of_name, it walks an object; an array is walked by its index in the path (rows/1, rows/2…) |
of_name (string as_path, long al_index) → string | The name of the al_index-th member (1-based) of the object at the path — to read the keys of an object whose shape is unknown. With of_count, it walks the object |
of_get_array (string as_path, ref string as_values[]) → long | Every element of the array at the path, in order, in as_values (emptied first) — each one as of_get_string reads it: a string comes back decoded, a number or a boolean as text, null as "", an object or an array as raw JSON (to of_load in another n_pbt_json). "" is the root. Returns the number of elements (0 for an empty array), -4 when there is no array at the path (nothing, an object, a scalar), -5 on an empty segment (a//b) |
of_pretty ( ) → string | The document — loaded or built — re-indented for reading (several lines, tabs) — a log, a text box. Empty when there is none |
of_pretty (long al_indent) → string | The document — loaded or built — re-indented with al_indent spaces per level (2 and 4 are the usual widths). 0 writes it compact, on one line and without a space: the smallest text to store or send. Empty when there is no document, or when al_indent is negative, above 16 or null. of_pretty() without an argument keeps the tabs |
// Local variables
n_pbt_json lnv_json
string ls_tags[]
string ls_compact
long ll_count
// Read every element of an array at once, then store the document on one line
lnv_json.of_load(/*json*/ '{"customer":{"name":"Ada","tags":["vip","b2b"]}}')
ll_count = lnv_json.of_get_array(/*path*/ "customer/tags", /*values*/ ls_tags) // 2 : vip, b2b
// The document on one line, without a space : the smallest text to store
ls_compact = lnv_json.of_pretty(/*indent*/ 0)
Modify a document #
The write side changes the loaded document in place, then of_text returns the result (compact) — what to save back. The setters replace the value at the path, or add the last key when it is missing from a parent object. Each returns 0, -4 when the parent is missing or of the wrong type, -5 on an empty path or an index 0 on an array. An array met on the way is crossed by an index that EXISTS (items/1/qty adds qty to the first element); only an object key is ever created. A null value (number, boolean, empty raw JSON) is written null.
| Member | Role |
|---|---|
of_set_string (string as_path, string as_value) → long | WRITES a string at as_path, escaped for you. The path may have SEVERAL levels (a/b/c): the missing ones are created as OBJECTS, and an EMPTY document becomes one — writing IS building. A value already there is replaced, wherever it sits. Returns 0, -4 when a level on the way is a scalar, or an array without that index (an array is crossed by an existing index: items/1/qty), -5 on an empty path, an empty segment or an index 0 on an array. A null string is written empty; for a JSON null: of_set_raw(as_path, "null") |
of_set_number (string as_path, long al_value) → long | WRITES a NUMBER at as_path, the decimal point forced whatever the region. Five overloads: integer, long, longlong, decimal, double — a decimal and a longlong keep EVERY digit (the cents of an amount, the last digit of an id above 2^53), and a literal such as 690.50 is a decimal, written 690.5. Same path rule and same codes as of_set_string; a null value writes null |
of_set_boolean (string as_path, boolean ab_value) → long | WRITES a BOOLEAN at as_path: true or false, never quoted. Same path rule and same codes as of_set_string; a null value writes null |
of_set_raw (string as_path, string as_json) → long | WRITES at as_path a value ALREADY in JSON — a nested object, an array, null — as it is: you vouch for it. This is also how an array is OPENED before of_add_*. Same path rule and same codes as of_set_string; a null or empty one writes null |
of_remove (string as_path) → long | Removes the value at the path — an object member (its key and one comma go with it) or an array element; the result stays valid. Returns 0, -4 when nothing is there, -5 on an empty path, an empty segment or an index 0 on an array |
// Local variables
n_pbt_json lnv_json
// Load, edit in place, then read back the ONE document with of_text
lnv_json.of_load(/*json*/ '{"name":"Ada","active":false}')
lnv_json.of_set_string(/*path*/ "name", /*value*/ "Grace") // replace
lnv_json.of_set_boolean(/*path*/ "active", /*value*/ true)
lnv_json.of_set_number(/*path*/ "count", /*value*/ 3) // added : the key was absent
// lnv_json.of_text() -> {"name":"Grace","active":true,"count":3}
Scope. JSON parses in pure PowerScript, no page or engine:
n_pbt_jsonstays autoinstantiate and works everywhere, PowerBuilder 10 included. For XML, see xml (which relies on the engine); to display a JSON as a collapsible tree, see jsontree.