0
0
PHPprogramming~20 mins

JSON encoding and decoding in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
JSON Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this PHP code using json_encode?
Consider the following PHP code snippet. What will it output?
PHP
<?php
$array = ['name' => 'Alice', 'age' => 30, 'active' => true];
echo json_encode($array);
?>
A{"name":"Alice","age":30,"active":true}
B{"name":"Alice","age":"30","active":"true"}
C{"name":Alice,"age":30,"active":true}
DArray
Attempts:
2 left
💡 Hint
Remember json_encode converts PHP arrays to JSON strings with proper quotes and types.
Predict Output
intermediate
2:00remaining
What does json_decode return with second parameter true?
What will be the output of this PHP code?
PHP
<?php
$json = '{"fruit":"apple","count":5}';
$result = json_decode($json, true);
var_dump($result);
?>
Aarray(2) { ["fruit"]=> string(5) "apple" ["count"]=> int(5) }
Bobject(stdClass)#1 (2) { ["fruit"]=> string(5) "apple" ["count"]=> int(5) }
Cstring(23) "{"fruit":"apple","count":5}"
DNULL
Attempts:
2 left
💡 Hint
The second parameter true makes json_decode return an associative array.
Predict Output
advanced
2:00remaining
What error does this PHP code raise when decoding invalid JSON?
What error or output will this PHP code produce?
PHP
<?php
$invalidJson = '{"name":"Bob", age: 25}';
$result = json_decode($invalidJson);
if (json_last_error() !== JSON_ERROR_NONE) {
    echo json_last_error_msg();
} else {
    var_dump($result);
}
?>
ANULL
BControl character error, possibly malformed UTF-8 characters
CSyntax error, malformed JSON
DSyntax error
Attempts:
2 left
💡 Hint
Look at the JSON string: keys must be quoted strings.
🧠 Conceptual
advanced
2:00remaining
Which option correctly encodes a PHP object to JSON with private properties?
Given a PHP class with private properties, which code snippet will correctly encode its public data to JSON?
PHP
<?php
class User {
    private string $name;
    private int $age;
    public function __construct(string $name, int $age) {
        $this->name = $name;
        $this->age = $age;
    }
    public function toArray(): array {
        return ['name' => $this->name, 'age' => $this->age];
    }
}
$user = new User('Eve', 28);
// Which line below correctly encodes the user data?
Aecho json_encode($user);
Becho json_encode($user->toArray());
Cecho json_encode(get_object_vars($user));
Decho json_encode((array)$user);
Attempts:
2 left
💡 Hint
Private properties are not accessible directly; use a method to expose data.
🚀 Application
expert
2:00remaining
How many items are in the array after decoding this JSON string?
Given this PHP code, how many items does the resulting array contain?
PHP
<?php
$json = '[{"id":1,"tags":["php","json"]},{"id":2,"tags":[]},{"id":3}]';
$array = json_decode($json, true);
echo count($array);
?>
A1
B2
C3
D0
Attempts:
2 left
💡 Hint
Count the top-level elements in the JSON array.