The json
vmod allows for JSON parsing. It can parse JSON strings, request body JSON,
and response body JSON.
vcl 4.0;
import json;
import std;
sub vcl_recv
{
std.cache_req_body(100KB);
json.parse_req_body();
if (json.is_valid() && json.is_object() &&
json.get("authorization")) {
req.http.X-authorization = json.get("authorization");
} else {
return(synth(401));
}
}
VOID parse(STRING json)
Parse a JSON string and create a new JSON context. Any previous context is freed.
Arguments:
json
accepts type STRINGType: Function
Returns: None
VOID parse_req_body()
Parse a request body and create a new JSON context. Can only be used in sub vcl_recv
.
std.cache_req_body()
must be called before using this. Any previous context is freed.
Arguments: None
Type: Function
Returns: None
VOID parse_resp_index()
Parse a response body and create a new JSON context. Must be used with edgestash.index_json()
.
Can only be used in sub vcl_deliver
. Any previous context is freed.
Arguments: None
Type: Function
Returns: None
BOOL contains(STRING element)
Traverse from the document root with a given path
. For example,
myobject.value
requires myobject
to be in the root, and value
to be a child.
You can’t search for arbitrary values without a full path.
This function is faster to call than get, and does not require workspace allocations.
Arguments:
element
accepts type STRINGType: Function
Returns: Bool
STRING get(STRING search, STRING error = 0, BOOL json_string = 0)
Get the value for path
in the JSON context.
For example, myobject.value
requires myobject
to be in the root, and value
to be a child.
Arguments:
search
accepts type STRING
error
accepts type STRING with a default value of 0
optional
json_string
accepts type BOOL with a default value of 0
optional
Type: Function
Returns: String
INT length()
Get the length of the root JSON node. For objects, its the number of keys. For arrays, its the number of elements. For everything else, its 0.
Arguments: None
Type: Function
Returns: Int
BOOL is_valid()
Is this valid JSON?
Arguments: None
Type: Function
Returns: Bool
BOOL is_error()
Is this invalid JSON?
Arguments: None
Type: Function
Returns: Bool
BOOL is_object()
Is this a JSON object?
Arguments: None
Type: Function
Returns: Bool
BOOL is_array()
Is this a JSON array?
Arguments: None
Type: Function
Returns: Bool
BOOL is_string()
Is this a JSON string?
Arguments: None
Type: Function
Returns: Bool
BOOL is_number()
Is this a JSON number?
Arguments: None
Type: Function
Returns: Bool
BOOL is_true()
Is this a JSON true value?
Arguments: None
Type: Function
Returns: Bool
BOOL is_false()
Is this a JSON false value?
Arguments: None
Type: Function
Returns: Bool
BOOL is_null()
Is this a JSON null value?
Arguments: None
Type: Function
Returns: Bool
STRING stringify(STRING value, BOOL quoted = 1)
Encode value into a JSON safe string
Arguments:
value
accepts type STRING
quoted
accepts type BOOL with a default value of 1
optional
Type: Function
Returns: String
VOID reset()
Reset the internal state.
Arguments: None
Type: Function
Returns: None
The json
VMOD is available in Varnish Enterprise version 6.0.6r6
and later.