MoneyWorks Manual
JSON_Get (JSON_object, id [, id, ... ])
Result type: text, number, or JSON object depending on JSON object contents
Definition: Extracts data or a sub-object from a JSON object obtained from JSON_Parse() or a previous call to JSON_Get() that yielded an object or array node JSON object. The extracted data is identified by the sequence of id identifiers, which will be textual id names, or, if the JSON structure being accessed is an array, a 0-based numeric index.
It is assumed that you know the structure of the JSON data in advance. If the element specified does not exist, the result will be NULL. When accessing an array by index, if the index is out of range, the return value will be NULL, thus you can iterate with an increasing index until you get a NULL result.
Prior to v9.2.5, references to subobjects/arrays returned by JSON_Get needed to be freed with JSON_Free. From 9.2.5 object references are of type TypeJSON and do not need to be freed. They are valid until the json handle that was returned by JSON_Parse is freed.
Example:
on AddJSONTransactions(json)
let t = 0
let jObj = JSON_Parse(json)
if not jObj // the JSON is bad
alert(GetLastErrorMessage())
return NULL
endif
foreach i in (0, 9999) // we don't know how big the array is; assume no more than this
let jtrans = JSON_Get(jObj, "transaction", i) // array index is 0-based
if jtrans == NULL // exit loop at end of array
break
endif
let n = JSON_Get(jtrans, "amount", "amount") // get amount element from amount subobject
JSON_Free(jtrans) // not reqd from v9.2.5
let t = t + n
endfor
JSON_Free(jObj) // handle returned from JSON_Parse must be freed
return t
end
See Also:
JSON_AsArray: Convert JSON object to an associative array
JSON_AsXML: Convert JSON to XML
JSON_Free: Free a parsed JSON structure or reference to a subobject thereof
JSON_GetArray: Return a JSON object as an associative array
JSON_Parse: Parse JSON text