Search
Varnish Cache Plus

JSON parsing (json)

Description

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

Example

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

API

parse

VOID parse(STRING json)

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

Arguments:

  • json accepts type STRING

Type: Function

Returns: None

parse_req_body

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

parse_resp_index

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

contains

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 STRING

Type: Function

Returns: Bool

get

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

length

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

is_valid

BOOL is_valid()

Is this valid JSON?

Arguments: None

Type: Function

Returns: Bool

is_error

BOOL is_error()

Is this invalid JSON?

Arguments: None

Type: Function

Returns: Bool

is_object

BOOL is_object()

Is this a JSON object?

Arguments: None

Type: Function

Returns: Bool

is_array

BOOL is_array()

Is this a JSON array?

Arguments: None

Type: Function

Returns: Bool

is_string

BOOL is_string()

Is this a JSON string?

Arguments: None

Type: Function

Returns: Bool

is_number

BOOL is_number()

Is this a JSON number?

Arguments: None

Type: Function

Returns: Bool

is_true

BOOL is_true()

Is this a JSON true value?

Arguments: None

Type: Function

Returns: Bool

is_false

BOOL is_false()

Is this a JSON false value?

Arguments: None

Type: Function

Returns: Bool

is_null

BOOL is_null()

Is this a JSON null value?

Arguments: None

Type: Function

Returns: Bool

stringify

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

Availability

The json VMOD is available in Varnish Cache Plus version 6.0.6r6 and later.