TechnoDiver Posted September 17, 2021 Share Posted September 17, 2021 I'm having a bit of trouble understanding why json_decode() is returning a null value, hoping someone has the time, will and energy to break it down for me. I've looked it up from various sources and don't get why it's not working. There's the following -> <?php public function hasPermission($key) { $group = $this->_db->get("groups", array("id", "=", $this->data()->group)); if($group->count()) { $permissions = $group->first()->permissions; echo gettype($permissions); echo $permissions; } } This returns $permissions as a string -> Quote {'admin': 1, 'moderator': 1} But when I do this -> <?php public function hasPermission($key) { $group = $this->_db->get("groups", array("id", "=", $this->data()->group)); if($group->count()) { $permissions = json_decode($group->first()->permissions, true); echo gettype($permissions); } } permissions comes back as a null type. I've been reading about json_decode() and it requires a json string in UTF-8 encoded. Which my data is (or seems to me to be). Could someone point out my error, please?! Thank you Quote Link to comment https://forums.phpfreaks.com/topic/313763-json_decodeencode-confusion/ Share on other sites More sharing options...
gw1500se Posted September 17, 2021 Share Posted September 17, 2021 (edited) From the manual: "Returns the value encoded in json in appropriate PHP type. Values true, false and null are returned as true, false and null respectively. null is returned if the json cannot be decoded or if the encoded data is deeper than the nesting limit." The value you are trying to decode is not a JSON encoded string. You need to translate the ' to ". Edited September 17, 2021 by gw1500se Quote Link to comment https://forums.phpfreaks.com/topic/313763-json_decodeencode-confusion/#findComment-1590044 Share on other sites More sharing options...
TechnoDiver Posted September 17, 2021 Author Share Posted September 17, 2021 9 minutes ago, gw1500se said: The value you are trying to decode is not a JSON encoded string. Ok, so I misread that as "a string in JSON format". Still saying that, then why does <?php $permissions = json_decode(json_encode($group->first()->permissions), true); return a string with the exact same value and type as $group->first()->permissions) shouldn't it be returning an array from the json? Quote Link to comment https://forums.phpfreaks.com/topic/313763-json_decodeencode-confusion/#findComment-1590045 Share on other sites More sharing options...
Solution Barand Posted September 17, 2021 Solution Share Posted September 17, 2021 This fails $j = "{'admin': 1, 'moderator': 1}" ; $a = json_decode($j, 1); echo '<pre> a ' . print_r($a, 1) . '</pre>'; This works $j = '{"admin": 1, "moderator": 1}' ; $b = json_decode($j, 1); echo '<pre> b ' . print_r($b, 1) . '</pre>'; Note the quotes in the JSON string. 1 1 Quote Link to comment https://forums.phpfreaks.com/topic/313763-json_decodeencode-confusion/#findComment-1590046 Share on other sites More sharing options...
gizmola Posted September 17, 2021 Share Posted September 17, 2021 You can call json_last_error() to see what the parser said. Quote Link to comment https://forums.phpfreaks.com/topic/313763-json_decodeencode-confusion/#findComment-1590047 Share on other sites More sharing options...
Barand Posted September 17, 2021 Share Posted September 17, 2021 My money's on "Syntax error". (Error 4) Quote Link to comment https://forums.phpfreaks.com/topic/313763-json_decodeencode-confusion/#findComment-1590048 Share on other sites More sharing options...
gizmola Posted September 17, 2021 Share Posted September 17, 2021 Agree 👍. Gotta use Double quotes around strings in Json. -> Spec Here. Scroll down to String. Embedded double quotes have to be escaped with a backslash. 1 Quote Link to comment https://forums.phpfreaks.com/topic/313763-json_decodeencode-confusion/#findComment-1590049 Share on other sites More sharing options...
TechnoDiver Posted September 18, 2021 Author Share Posted September 18, 2021 11 hours ago, Barand said: Note the quotes in the JSON string. Interesting, I find that a bit counterintuitive as I've made a habit of keeping singles inside doubles. Thanks for your help Quote Link to comment https://forums.phpfreaks.com/topic/313763-json_decodeencode-confusion/#findComment-1590057 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.