Skip to main content

How to Decode JSON into an Object in PHP with JWT::jsonDecode

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

PHP's json_decode returns null when the JSON is broken, which is easy to miss. php-jwt's public helper JWT::jsonDecode decodes JSON into an object too, but throws a DomainException with the reason when the JSON is invalid.

Syntax​

Syntax
use Firebase\JWT\JWT;

$<object> = JWT::jsonDecode(<json>);
$<value> = $<object>-><property>;
PartWhat it does
JWT::jsonDecode(<json>)Decodes the JSON text into a stdClass object (or another value for non-object JSON).
$<object>-><property>Reads one property of the object.
DomainExceptionThrown when the text isn't valid JSON.

php-jwt's source documents jsonDecode as: "Decode a JSON string into a PHP object." and "@throws DomainException Provided string was invalid JSON". It calls json_decode with large integers kept as strings, so no precision is lost.

The Example​

The example decodes a small set of JWT-style claims, prints the role and exp properties, then decodes a piece of cut-off JSON inside try and prints the exception.

Prerequisites​

  • PHP and Composer. This guide uses PHP 8.6.0 and Composer 2.11.3.

Step 1: Create the Project​

Terminal
mkdir demo
cd demo
composer require firebase/php-jwt

composer require downloads php-jwt, version 7.2.2 here, into a vendor folder and writes vendor/autoload.php.

Step 2: Write the Code​

Create main.php in the demo folder:

main.php, which decodes claims with sub ada, role reader and exp with JWT::jsonDecode, echoes the role and exp properties, then decodes cut-off JSON inside try and echoes the DomainException's class and message.

main.php
<?php

require __DIR__ . '/vendor/autoload.php';

use Firebase\JWT\JWT;

$claims = JWT::jsonDecode('{"sub":"ada","role":"reader","exp":1764550800}');

echo $claims->role, "\n";
echo $claims->exp, "\n";

try {
JWT::jsonDecode('{"sub":');
} catch (DomainException $e) {
echo get_class($e), ': ', $e->getMessage(), "\n";
}

Step 3: Run It​

Terminal
php main.php
Output
reader
1764550800
DomainException: Syntax error, malformed JSON

A zsh terminal where php main.php prints reader, 1764550800, and DomainException: Syntax error, malformed JSON.

The command is the same in zsh, bash and PowerShell.

What Happened​

  • JWT::jsonDecode turned the claims into an object, and ->role and ->exp read two of its properties.
  • The cut-off JSON made it throw DomainException with the reason, Syntax error, malformed JSON, which the catch block printed.

Common Mistakes​

MistakeWhat happensDo this instead
Using json_decode and not checking for nullA broken input quietly becomes null and fails laterUse JWT::jsonDecode, or json_decode with JSON_THROW_ON_ERROR
Reading properties with ['role']jsonDecode returns an object, not an arrayUse ->role
Expecting the JSON text null to throwJWT::jsonDecode('null') returned NULL without an exceptionCheck that the result is an object

Troubleshooting​

SymptomLikely causeFix
DomainException: Syntax error, malformed JSONThe input isn't valid JSONCheck the text, for example a cut-off piece
Class "Firebase\JWT\JWT" not foundThe autoloader isn't loadedrequire __DIR__ . '/vendor/autoload.php';

FAQ​

Is this the same as reading a JWT's claims?​

It's how you'd turn a decoded payload into an object, after JWT::urlsafeB64Decode. That reads the claims without verifying them; to trust them, verify the token with JWT::decode and the key.

Why does php-jwt keep big integers as strings?​

So that very large numbers in a token don't lose precision by turning into floats.

Conclusion​

JWT::jsonDecode($json) decodes JSON into an object whose properties you read with ->, and throws a DomainException with the reason when the JSON is invalid, where plain json_decode would return null.

Additional Resources​