parse_json()

Purpose

parse_json(str, container)

Parses the json string str and places the resulting json object into container where container can be any valid rsyslog variable. Note that the entire str must be valid JSON for the function to succeed. If there is trailing data after a valid JSON object/value, it will be considered as an error. Returns 0 on success and something otherwise if str does not contain a valid, complete json string.

Example

In the following example the json string is placed into the variable $!parsed. The output is placed in variable $.ret

set $.ret = parse_json("{ \"c1\":\"data\" }", "\$!parsed");

Parsed arrays can be rendered directly with JSON-aware templates:

set $.ret = parse_json("[\"Content Server\", \"Internet Services\"]",
                       "\$!url_categories");

template(name="out" type="list" option.jsonf="on") {
    property(outname="UrlCategories" name="$!url_categories" format="jsonf")
}

This emits UrlCategories as a JSON array, not as a quoted string.


Support: rsyslog Assistant | GitHub Discussions | GitHub Issues: rsyslog source project

Contributing: Source & docs: rsyslog source project

© 2008–2026 Rainer Gerhards and others. Licensed under the Apache License 2.0.