json_loads
The `json_loads` directive loads a string representation of JSON into a variable for later use.
Description
The json_loads directive is part of the NGINX JSON module and is utilized to load a string, potentially containing variables, into a designated JSON variable. When the directive is invoked, it processes the provided string, attempting to parse it as JSON using the Jansson library's json_loadb function. If the parsing is successful, the resulting JSON object is stored in memory and associated with the specified variable. This facilitates further operations on the JSON content, which can be accessed using related directives.
The directive's syntax allows for two arguments: the first specifies the variable in which to store the JSON data, while the second is the string containing the JSON itself. The loading process logs any errors encountered during parsing, providing feedback for debugging. In scenarios where the supplied string fails to be parsed into valid JSON, an error is logged, and the operation is aborted. This robust error handling ensures that erroneous input does not inadvertently lead to undefined behavior within the application.
Additionally, since JSON objects can have complex nested structures, this directive can excel when combined with other directives like json_dump, allowing users to serialize or manipulate JSON structures efficiently. It's crucial to ensure that the input string conforms to JSON format to avoid runtime errors.
Config Example
http {
json_load $input_json;
}The input string must be a valid JSON format, or an error will be logged and processing halted.
Variables used in the string should be defined prior to using json_loads, or they will not resolve correctly.