JSON Required Keys
PHP
Hard
5 views
Problem Description
JSON object must contain keys id and name. Print OK, MISSING_KEYS, or INVALID_JSON.
Input Format
One line json.
Output Format
One word status.
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;
}
if(!array_key_exists('id',$obj) || !array_key_exists('name',$obj)) echo 'MISSING_KEYS';
else echo 'OK';
?>
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!