Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

PHP stops when you echo or concatenate a decoded JSON object

ErrorObject of class stdClass could not be converted to string

Short answer

You used an object where PHP wants text, such as echo, string concatenation or an SQL value. json_decode returns stdClass objects by default. Read a property with the arrow, or decode with the associative flag set to true and use array keys.

  • Applies to PHP 8.3, PHP 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Dump the value that fails. If it prints object(stdClass), you hold the whole object and need one of its properties.

    var_dump($value);
  2. 2Match what you see, then jump to its fix

    1. 1var_dump shows object(stdClass) for a value that came from json_decode.json_decode returned an object and you treated it as a stringFix
    2. 2Code uses $row['name'] and the error appears on a decoded API response.You wanted an array but did not ask for oneFix
    3. 3The top-level key works but the failing value is a child of it.A nested value is still an objectFix

What does the error look like?

  • Object of class stdClass could not be converted to string
  • Fatal error: Uncaught Error: Object of class stdClass could not be converted to string

Cause 1 of 3

json_decode returned an object and you treated it as a string

1Check

With no second argument, json_decode turns every JSON object into stdClass. Concatenating or echoing that object fails.

$data = json_decode($json);
var_dump($data);

2Fix

Read the property with the arrow operator. For keys that are not valid names, such as foo-bar, use braces and quotes.

echo $data->name;
echo $data->{'foo-bar'};

3Verify

The echo prints text instead of a fatal error.

php -r "echo json_decode('{\"name\":\"Ada\"}')->name;"

Confirmed by php.net

Did this fix it?

Cause 2 of 3

You wanted an array but did not ask for one

1Check

Passing true as the second argument returns associative arrays instead of objects. Passing null leaves it to JSON_OBJECT_AS_ARRAY, which is not set by default.

php -r "var_dump(json_decode('{\"a\":1}', true));"

2Fix

Decode as arrays everywhere or use arrows everywhere; do not mix the two in one code path. Add JSON_THROW_ON_ERROR so failures do not hide as null.

$data = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
echo $data['name'];

3Verify

The array key reads without an error.

php -r "echo json_decode('{\"name\":\"Ada\"}', true)['name'];"

Confirmed by php.net

Did this fix it?

Cause 3 of 3

A nested value is still an object

1Check

Without the associative flag every level becomes stdClass, not just the first. A child object cannot be placed into a string either.

var_dump($data->user);

2Fix

Go one level deeper to the scalar you need, or re-encode the child if you really need JSON text.

echo $data->user->email;
echo json_encode($data->user);

3Verify

The line prints a string with no error.

php your-script.php

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Pick one decode style (arrays or objects) for the project and wrap json_decode in one helper.
  • Use JSON_THROW_ON_ERROR so a bad payload fails loudly.
  • Add static analysis (PHPStan or Psalm) to catch string use of objects before runtime.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

Apache returns 403 when a URL contains %3F

a request like /page%3Fname=test is refused while /page?name=test works

Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.