{definejson:name:expression}

Description

Stores a parsed JSON value under a name so later expressions can read parts of it. The expression you pass - a literal JSON string, or anything that produces JSON such as an include of a remote endpoint or a view that outputs JSON - is JSON-decoded once and kept in the page-global content cache under the key definejson:name. You read it back with jsonpath:name:query, not with var:name (var reads a separate plain-text store that define writes). Like define, definejson prints nothing - it returns an empty string; its only effect is storing the value. The command token is case-insensitive, so definejson and defineJSON are the same command, but the variable name you choose is case-sensitive. If the expression is not valid JSON it is decoded to an empty array, with no error. The definejson must run before the jsonpath that reads it on the same page.

Parameters

name required

The variable name to store the parsed JSON under. Read it back later with jsonpath using the same name. The name is case-sensitive. It is stored page-wide, so a name defined anywhere on the page is visible to every later jsonpath on that page.

expression required

A JSON string, or any expression that produces JSON - a literal JSON object or array, an include of a remote JSON endpoint, or a view that outputs JSON. It is JSON-decoded once before storing. Invalid JSON is decoded to an empty array, with no error.

Examples

test[{definejson:greeting:{"msg":"hello"}}]
Expected[]
Actual[]
Like define, definejson stores the value and returns an empty string. Here the brackets show that the command itself outputs nothing.
test[{definejson:bad:not json}{jsonpath:bad:$.anything}]
Expected[]
Actual[]
If the expression is not valid JSON it is decoded to an empty array, silently, with no error. A later jsonpath query then matches nothing and returns empty. This is a common trap: a typo in the JSON yields no visible failure, just empty results.
test{definejson:people:{"items":[{"n":"Ada"},{"n":"Boris"}]}}{jsonpath:people:$.items[*].n::, }
ExpectedAda, Boris,
ActualAda, Boris,
A jsonpath query that matches several values joins them with the fourth jsonpath argument as delimiter. Note jsonpath appends the delimiter after every match, including the last, so the output ends with a trailing comma and space.
virtual{definejson:JsonRet:{view:ecomail_subscr}}{jsonpath:JsonRet:$.data[*].email::, }
Expected(a comma-separated list of the emails the view returned)
The expression can be anything that produces JSON, such as a view that outputs a JSON document, or an include of a remote JSON endpoint. definejson decodes it once and stores it, so several later jsonpath queries reuse the same parsed structure without re-fetching. Output depends on the live view, so this example is illustrative.
test[{definejson:scores:[10,20,30]}{jsonpath:scores:$[1]}]
Expected[20]
Actual[20]
The expression can be a JSON array as well as an object. jsonpath then addresses elements by index, counting from zero.
test[{definejson:address:{"city":"Praha","zip":"11000"}}{jsonpath:address:$.city}]
Expected[Praha]
Actual[Praha]
The canonical pairing: definejson stores the parsed JSON under a name, then jsonpath:name:query reads it back. Use jsonpath, not var (var reads a separate store set by define).