Skip to main content

Introduction

The json_encode filter converts a variable—such as an array or object—into a JSON-formatted string. This filter is especially useful when you need to pass data from a Twig template to a JavaScript script or an API that consumes JSON.

Example of Use

Example of how to use the json_encode filter to convert an array into a JSON string:

Output Example of the Code

Result of `json_encode` Filter