Varnish Cache Plus

JSON parsing (json)

Description

vmod-json allows for JSON parsing. It can parse JSON strings, request body JSON, and response body JSON.

Example VCL

Request Body Parsing

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));
    }
}

Functions

parse

VOID parse(STRING json)

  • Description

    Parse a JSON string and create a new JSON context. Any previous context is freed.

  • Return value

    None

    • json

      A JSON string.

parse_req_body

VOID parse_req_body()

  • Description

    Parse a request body and create a new JSON context. Can only be used in vcl_recv. std.cache_req_body() must be called before using this. Any previous context is freed.

  • Return value

    None

parse_resp_index

VOID parse_resp_index()

  • Description

    Parse a response body and create a new JSON context. Must be used with edgestash.index_json(). Can only be used in vcl_deliver. Any previous context is freed.

  • Return value

    None

get

STRING get(STRING path, STRING error, BOOL json_string)

  • Description

    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.

  • Return value

    The resulting JSON value. If not found, error.

    • path

      A JSON path. Supports object (.) and array ([0]) notation.

    • error

      Return value if search is not found.

    • json_string

      Return a JSON encoded string. This string can be reparsed as valid JSON. Defaults to false. Ex: My "string" will be encoded as "my \"string\"".

contains

BOOL contains(STRING path)

  • Description

    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.

  • Return value

    Returns true if the given path exists.

    • path

      A JSON search string. Supports object (.) and array ([0]) notation.

length

int length()

  • Description

    Get the length of a JSON context.

  • Return value

    If the context is an object, the number of keys. If its an array, the number of elements. Otherwise 0.

is_valid

BOOL is_valid()

  • Description

    Is the JSON context valid JSON?

  • Return value

    true if the context contains valid JSON. Otherwise false.

is_error

BOOL is_error()

  • Description

    Is the JSON context invalid JSON?

  • Return value

    true if the context contains invalid JSON. Otherwise false.

is_object

BOOL is_object()

  • Description

    Is the context a JSON object?

  • Return value

    true if the context is a JSON object. Otherwise false.

is_array

BOOL is_array()

  • Description

    Is the context a JSON array?

  • Return value

    true if the context is a JSON array. Otherwise false.

is_string

BOOL is_string()

  • Description

    Is the context a JSON string?

  • Return value

    true if the context is a JSON string. Otherwise false.

is_number

BOOL is_number()

  • Description

    Is the context a JSON number?

  • Return value

    true if the context is a JSON number. Otherwise false.

is_true

BOOL is_true()

  • Description

    Is the context a JSON true?

  • Return value

    true if the context is a JSON true. Otherwise false.

is_false

BOOL is_false()

  • Description

    Is the context a JSON false?

  • Return value

    true if the context is a JSON false. Otherwise false.

is_null

BOOL is_null()

  • Description

    Is the context a JSON null?

  • Return value

    true if the context is a JSON null. Otherwise false.

stringify

STRING stringify(STRING value, BOOL quoted = true)

  • Description

    Turn value into a JSON safe string.

  • Return value

    value encoded as a JSON string.

    • quoted

      Return encoded string with surrounding quotes. Defaults to true.