JSON::parse

Description

Parses JSON content into a JSON cache that can be manipulated using further JSON:: commands.
  • If a string is omitted, returns any JSON cache that preexists in the context in which this is executed. This is the normal case when the command is executed in the JSON_REQUEST or JSON_RESPONSE event.
  • If a string is provided, it is assumed to contain JSON and is parsed into a new JSON cache. This will be deleted when it is no longer referenced by a Tcl variable. This is useful when a JSON profile is not being used.
  • If the max entries parameter is provided, the parser is limited to 3 times that number of syntactic tokens, which should usually accomodate at least that number of entries, where an ‘entry’ corresponds roughly to a key:value pair or and array index.
  • Fails if there is a syntax error or the number of tokens exceeds the limit.

Note

It is not necessary to provide a string when this command is used with a JSON profile in the JSON_REQUEST or JSON_RESPONSE event or an event that is raised after them. At that point there is already a JSON cache with the parsed JSON content from the message body.

Syntax

JSON::parse [JSON_STRING] [MAX_ENTRIES]

Examples

when JSON_REQUEST {
    JSON::render
}
when JSON_REQUEST {
    set rootval [JSON::root]
    set a_bool [JSON::get $rootval boolean]
}
when HTTP_REQUEST_DATA {
    set body ...
    set cache [JSON::parse $body]
    JSON::render $cache
}