PHP stops when you echo or concatenate a decoded JSON object
Object of class stdClass could not be converted to stringShort 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.
Fix it fast
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);2Match what you see, then jump to its fix
- 1
var_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
Code uses $row['name'] and the error appears on a decoded API response.You wanted an array but did not ask for oneFix - 3
The top-level key works but the failing value is a child of it.A nested value is still an objectFix
- 1
What does the error look like?
Object of class stdClass could not be converted to stringFatal 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
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
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.phpConfirmed by php.net
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
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack 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 fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fixBack end
AWS SDK for PHP rejects requests: the signature is from the future
Signature not yet current: {date} is still later than {date}
AWS compares the timestamp in your signed request with its own clock.
2 causes, each with a fix