All the properties of your object are private. aka... not available outside their class's scope.

Solution for PHP >= 5.4

Use the new JsonSerializable Interface to provide your own json representation to be used by json_encode

class Thing implements JsonSerializable {
    ...
    public function jsonSerialize() {
        return [
            'something' => $this->something,
            'protected_something' => $this->get_protected_something(),
            'private_something' => $this->get_private_something()
        ];
    }
    ...
}

Solution for PHP < 5.4

If you do want to serialize your private and protected object properties, you have to implement a JSON encoding function inside your Class that utilizes json_encode() on a data structure you create for this purpose.

class Thing {
    ...
    public function to_json() {
        return json_encode(array(
            'something' => $this->something,
            'protected_something' => $this->get_protected_something(),
            'private_something' => $this->get_private_something()                
        ));
    }
    ...
}

A more detailed writeup

Answer from jondavidjohn on Stack Overflow
๐ŸŒ
PHP
php.net โ€บ manual โ€บ en โ€บ function.json-encode.php
PHP: json_encode - Manual
Like the reference JSON encoder, json_encode() will generate JSON that is a simple value (that is, neither an object nor an array) if given a string, int, float or bool as an input value.
Discussions

Help with Azure REST API json encode/decode
Hard to say without actually seeing the data, but if something is changed, then visually compare it first. My -guess- is that Azure's sending back an array with keyed values that happen to be numeric keys that are converted to a consecutive array. Run this to see what I mean: $json = '{"0":"a","1":"b","2":"c"}'; echo "ORIGINAL:\n".$json . "\n";; $x = json_decode($json,true); var_dump($x); echo "RE-ENCODED:\n"; echo json_encode($x); More on reddit.com
๐ŸŒ r/PHPhelp
5
1
July 6, 2022
What is the appropriate way to encode an array into JSON?
If it's just scalar values in your array, you may as well use ext-json; that's what the JsonEncoder uses anyway IIRC. If there are objects involved, or you want to transform it in any way, you probably want to use the serializer component: https://symfony.com/doc/current/components/serializer.html More on reddit.com
๐ŸŒ r/symfony
3
1
April 24, 2023
Json_encode adds backslashes to a PHP object even when JSON_UNESCAPED_SLASHES is used
I think you are double encoding. You have JSON is string, then json encode that string. I think what you want is one PHP data structure (probably an array) with all the data then simply encode that php data into JSON. Try somthing like this; [ 'data' => [ 'apiRequest' => [ 'signature' => 'I3d4b8680011fa544e9d5dab9f3', ], ], ], 'status' => 'success', ]; var_dump($message); $json = json_encode($message, JSON_UNESCAPED_SLASHES); var_dump($json); More on reddit.com
๐ŸŒ r/PHPhelp
5
1
November 30, 2021
[PHP] TIL sparse arrays are json_encoded into objects instead of arrays
Can't you just use the option to return as an associative array? for example: json_decode($array,true) More on reddit.com
๐ŸŒ r/ProgrammerTIL
5
33
August 16, 2016
People also ask

Why does json_encode return false?
Usually because a string is not valid UTF-8, or the data contains NAN or INF. Call jsonlasterrormsg() to see the reason, or pass JSONTHROWONERROR so it throws a JsonException instead of returning false.
๐ŸŒ
coddy.tech
coddy.tech โ€บ docs โ€บ php โ€บ json_encode()
PHP json_encode(): Convert Arrays and Objects to JSON
How do I convert a PHP array to JSON?
Pass it to jsonencode(): jsonencode(['name' =&gt; 'Ada', 'age' =&gt; 36]) returns {"name":"Ada","age":36}. An array with keys 0, 1, 2... in order becomes a JSON array instead: json_encode(['a', 'b']) returns ["a","b"].
๐ŸŒ
coddy.tech
coddy.tech โ€บ docs โ€บ php โ€บ json_encode()
PHP json_encode(): Convert Arrays and Objects to JSON
How do I make json_encode output {} instead of [] for an empty array?
An empty PHP array always encodes as []. Use new stdClass() or (object) [] for an empty object, or the JSONFORCEOBJECT flag, which turns every array in the value into an object.
๐ŸŒ
coddy.tech
coddy.tech โ€บ docs โ€บ php โ€บ json_encode()
PHP json_encode(): Convert Arrays and Objects to JSON
๐ŸŒ
ReqBin
reqbin.com โ€บ code โ€บ php โ€บ 4qfsaffq โ€บ php-json-encode-example
How do I encode a PHP object to JSON string?
July 5, 2023 - To encode a PHP object to a JSON formatted string, you can use the json_encode($value, $options, $depth) function. The $value parameter specifies the PHP object to encode.
๐ŸŒ
W3Schools
w3schools.in โ€บ json โ€บ json-php
Encoding and Decoding of Json Objects Through PHP
PHP json_decode and json_encode functions are used to decode and encode JSON object.
๐ŸŒ
Scout APM
scoutapm.com โ€บ home โ€บ blog โ€บ php json_encode: serialize php objects to json
PHP Json_encode: Serialize PHP Objects to JSON | Scout Monitoring
May 1, 2020 - Since the information in JSON is stored in key/value pairs,*** json_encode()*** is more likely to be used to encode PHP objects and their instance variables.
Find elsewhere
๐ŸŒ
Coddy.Tech
coddy.tech โ€บ docs โ€บ php โ€บ json_encode()
PHP json_encode(): Convert Arrays and Objects to JSON
3 weeks ago - json_encode($value) turns a PHP array, object or scalar into a JSON string.
๐ŸŒ
PHPHub
phphub.net โ€บ home โ€บ json_encode online
json_encode PHP online
May 10, 2025 - The json_encode function in PHP allows you to convert PHP data (like arrays or objects) into a JSON string.
๐ŸŒ
Jobtensor
jobtensor.com โ€บ Tutorial โ€บ PHP โ€บ en โ€บ JSON
PHP JSON - json_encode(), json_decode() | jobtensor
<?php $countries = array("Mark" => "USA", "Raymond" => "UK", "Jeff" => "JPN", "Mike" => "DE"); echo json_encode($countries); The json_decode() function is used to decode a JSON object into a PHP associative array or object. It has an optional second parameter that gives a PHP associative array instead of the default PHP object.
๐ŸŒ
PHPpot
phppot.com โ€บ php โ€บ php-json-encode-and-decode
PHP JSON Encode and Decode - PHPpot
For PHP JSON encodes, the following list of constants will be used for the options parameter of the json_encode() function. JSON_HEX_TAG โ€“ Used to encode HTML content by replacing < and > symbol with \u003C and \u003E.
๐ŸŒ
Tutorial Republic
tutorialrepublic.com โ€บ php-tutorial โ€บ php-json-parsing.php
How to Encode and Decode JSON Data in PHP - Tutorial Republic
You can also force json_encode() function to return an PHP indexed array as JSON object by using the JSON_FORCE_OBJECT option, as shown in the example below:
๐ŸŒ
Carmatec
carmatec.com โ€บ home โ€บ understanding php json_encode() and json_decode()
Understanding PHP json_encode() and json_decode() - Carmatec
3 weeks ago - The json_encode() function in PHP converts a PHP variable (such as an array or object) into a JSON-formatted string. Conversely, json_decode() takes a JSON string and converts it back into a PHP variable, typically an array or object.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ json โ€บ json_php_example.htm
JSON with PHP
PHP json_encode() function is used for encoding JSON in PHP. This function returns the JSON representation of a value on success or FALSE on failure. ... options โˆ’ This optional value is a bitmask consisting of JSON_HEX_QUOT, JSON_HEX_TAG, ...
๐ŸŒ
W3Schools
w3schools.com โ€บ php โ€บ php_json.asp
PHP and JSON
Since the JSON format is a text-based ... First, we will look at the following two functions: ... The json_encode() function is used to encode a value to JSON format....
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ php json_encode() function
PHP json_encode() Function - Scaler Topics
March 31, 2024 - This is useful to prevent potential ... 2.5x salary growth with average post-Scaler CTC reaching โ‚น23L. ... The json_encode() function in PHP returns a JSON-encoded string representing the provided PHP value....
๐ŸŒ
Code Beautify
codebeautify.org โ€บ json-encode-online
JSON Encode Online to encode JSON from stdClass Object of PHP to readable form.
JSON Encode Online is easy to use tool to encode JSON data, which converts stdClass Object of PHP to JSON.
๐ŸŒ
PHP Tutorial
phptutorial.net โ€บ home โ€บ php oop โ€บ php json
An Essential Guide to PHP JSON - PHP Tutorial
July 21, 2021 - Third, create a new Person object and serialize it to JSON data using the json_encode() function. JSON is a lightweight data-interchange format. Use the json_encode() function to convert PHP variables to JSON.