Jump to content

json_decode/encode confusion


TechnoDiver
 Share

Go to solution Solved by Barand,

Recommended Posts

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

Link to comment
Share on other sites

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 by gw1500se
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

  • Solution

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.

  • Like 1
  • Great Answer 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.