JSON Key Count
PHP
Easy
6 views
Problem Description
Given a JSON object. If invalid, print INVALID_JSON. Else print how many keys it has.
Input Format
One line json.
Output Format
One integer or INVALID_JSON.
Official Solution
<?php
$js=trim(stream_get_contents(STDIN));
if($js==='') exit;
$obj=json_decode($js,true);
if(json_last_error()!==JSON_ERROR_NONE || !is_array($obj)){
echo 'INVALID_JSON';
exit;
}
echo count($obj);
?>
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!