Json Encode
3 min read
Jun 2, 2026
---
title: json_encode
autoseo:
enabled: true
theme: learn2
child_type: default
admin:
children_display_order: collection
visible: true
---
json_encode — Transforms a value into a valid JSON string.
Basic Usage:{$user|json_encode}
Depending on the value of $user this would return a string in JSON-format, e.g. {"username":"tplix","email":"[email protected]"}.
| Parameter | Type | Required | Description |
|---|---|---|---|
| 1 | int | No | bitmask of flags, directly passed to PHP's json_encode |
| 2 | int | No | Depth |
If you curious of the numeric values of the constants, as of JSON 1.2.1, the constants have the following values (not that you should use the numbers directly):
Display Numeric ValuesBy passing 16 as the second parameter, you can force json_encode to always format the JSON-string as an object. Without it, an array $myArray = ["a","b"] would be formatted as a javascript array:
{$myArray|json_encode} # renders: ["a","b"]
{$myArray|json_encode:16} # renders: {"0":"a","1":"b"}
Leave a Comment