0

I am having trouble converting a basic valid JSON string object into a PHP object using json_decode (it shows as null). I have tried several things but so far still not working. I'm sure I am missing something simple but I could use help getting this going. This seems to be an issue about the JSON string that is coming directly from the database (text field) - possibly something related to escaping characters or something?

basic json string in database (text field) :

{"address":"111 main street","id":"1","name":"test","phone":"888 888 8888","zip":"123"}

PHP

$jsonobj = $dbfield;
echo $jsonobj

on page display is:

string(187) "{"address":"111 main street","id":"1","name":"test","phone":"888 888 8888","zip":"123"}"
var_dump($jsonobj);

on page display is:

string(187) "{"address":"111 main street","id":"1","name":"test","phone":"888 888 8888","zip":"123"}"
php
var_dump(json_decode($jsonobj));

on page display is: NULL - why? php var_dump(html_entity_decode($jsonobj)); on page display is: string(87) "{"address":"111 main street","id":"1","name":"test","phone":"888 888 8888","zip":"123"}" this works when i create hard coded var...

$js = '{"address":"111 main street","id":"1","name":"test","phone":"888 888 8888","zip":"123"}';
$obj = json_decode($js);
echo $obj->address;

display works

111 main street

Can anyone shed some light on converting what looks like valid json from the database to php object?

3
  • 2
    Notice the character count 187 vs the actual character count, which is about 88. You have some invisible characters breaking your json string. This may be from when you're inserting it into the database Commented Mar 7, 2024 at 17:06
  • Thanks for the help. As it turns out – I needed to use both html_entity_decode and json_decode to get this working like this: $jsonobj = json_decode( html_entity_decode($dbfield)); Thanks for the help - Dave Commented Mar 7, 2024 at 18:28
  • You should not have applied HTML encoding to the data that was inserted into the data base, in the first place. This should happen when you output the data into an HTML context. Commented Mar 8, 2024 at 7:22

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.