[wp-trac] [WordPress Trac] #54484: REST API returns (empty) array for Meta but Schema has type object
WordPress Trac
noreply at wordpress.org
Thu Mar 27 21:22:14 UTC 2025
#54484: REST API returns (empty) array for Meta but Schema has type object
--------------------------+-----------------------
Reporter: dtrunk90 | Owner: flixos90
Type: defect (bug) | Status: assigned
Priority: normal | Milestone: 6.9
Component: REST API | Version: 4.7
Severity: normal | Resolution:
Keywords: | Focuses:
--------------------------+-----------------------
Changes (by flixos90):
* owner: (none) => flixos90
* status: new => assigned
* version: 5.8.2 => 4.7
* milestone: Awaiting Review => 6.9
Comment:
I realize I am finding this ticket ''very late'', but thank you for
raising this @dtrunk90!
The type of `meta` is `object`, so that's all correct. But the problem is
that an empty array in PHP becomes `[]` when JSON-encoded, it doesn't make
a difference whether it's supposed to be a regular (indexed) array (`[]`
in JS/JSON) or an associative array (`{}` in JS/JSON). So this is
certainly a bug.
The workaround is to either use `JSON_FORCE_OBJECT` when JSON-encoding the
data, or transform the empty array to an empty object (e.g. via `new
stdClass()`).
I think the latter fix is what's needed here, since it has to be
determined for each specific possibly empty array individually, based on
the JSON schema of the data. I'm going to take a closer look at this.
This has been a bug ever since the REST API was introduced, therefore
setting the version to `4.7`.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/54484#comment:3>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list