# perldoc > JSON::MaybeXS

---
type: CommandReference
command: JSON::MaybeXS
mode: perldoc
section: 
source: perldoc
---

## Quick Reference
- `use JSON::MaybeXS;` — imports `encode_json`, `decode_json`, and `JSON` constant.
- `my $data = decode_json($json_text);` — deserialize JSON to Perl.
- `my $json = encode_json($data);` — serialize Perl to JSON.
- `my $json_obj = JSON::MaybeXS->new(utf8 => 1, pretty => 1);` — create an object with options.
- `my $json_obj = JSON()->new;` — obtain the backend class name and instantiate.
- `my $true = JSON()->true;` — create a JSON boolean `true`.
- `my $false = JSON()->false;` — create a JSON boolean `false`.
- `use JSON::MaybeXS ':all';` — import `encode_json`, `decode_json`, `is_bool`.

## Name
`JSON::MaybeXS` — Use `Cpanel::JSON::XS` with a fallback to `JSON::XS` and `JSON::PP`

## Synopsis
perl
use JSON::MaybeXS;

my $data_structure = decode_json($json_input);
my $json_output    = encode_json($data_structure);

my $json           = JSON()->new;
my $json_with_args = JSON::MaybeXS->new(utf8 => 1);  # or { utf8 => 1 }
## Options

### Exported Symbols
- `encode_json` — Encodes a Perl data structure to JSON text.
- `decode_json` — Decodes JSON text into a Perl data structure.
- `JSON` — Returns the loaded backend class name (e.g., `Cpanel::JSON::XS` or `JSON::PP`) for calling `new`.
- `is_bool` — Returns true if the scalar is a JSON boolean (`true`/`false`). Available since v1.002004. Exported on request.
- `to_json` / `from_json` — Legacy aliases for `encode_json` / `decode_json`; included only via `:legacy` import tag.

### Import Tags
- `:all` — imports `encode_json`, `decode_json`, `is_bool`.
- `:legacy` — imports `:all` plus `to_json` and `from_json`. Avoid in new code.

### Constructor
- `new(%options)` — Factory method that returns an object blessed into the selected backend. Accepts a hash or hashref of constructor options (e.g., `utf8`, `pretty`). Equivalent to `$class->new->utf8(1)->pretty(1)` on the backend.

### Boolean Constants
- `JSON()->true` / `JSON()->false` — Return JSON booleans. Also callable as `JSON::MaybeXS::true` / `JSON::MaybeXS::false` or as methods on `JSON::MaybeXS`.

## Examples

### Basic encode/decode
perl
use JSON::MaybeXS;
my $json = encode_json({ key => 'value' });
my $data = decode_json('{"key":"value"}');
### Object with options
perl
my $json = JSON::MaybeXS->new(utf8 => 1, pretty => 1);
my $json_text = $json->encode($data);
### Using booleans
perl
use JSON::MaybeXS;
my $data = { active => JSON()->true, deleted => JSON()->false };
my $json = encode_json($data);
### Migrating from JSON::Any
perl
# Instead of JSON::Any->new->objToJson($data):
use JSON::MaybeXS;
my $json = encode_json($data);

# Instead of JSON::Any->new->jsonToObj($json):
my $data = decode_json($json);
## See Also
- [`Cpanel::JSON::XS`](https://www.chedong.com/phpMan.php/perldoc/Cpanel%3A%3AJSON%3A%3AXS/markdown)
- [`JSON::XS`](https://www.chedong.com/phpMan.php/perldoc/JSON%3A%3AXS/markdown)
- [`JSON::PP`](https://www.chedong.com/phpMan.php/perldoc/JSON%3A%3APP/markdown)
- [`JSON`](https://www.chedong.com/phpMan.php/perldoc/JSON/markdown)
- [`JSON::Any`](https://www.chedong.com/phpMan.php/perldoc/JSON%3A%3AAny/markdown)