We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

getJsonRawBody, objects or arrays?

I use Phalcon with Angular. It seems a good match. Angular has a disposition to send POST requests with a JSON string in the request body. Phalcon seems to have that covered with the request->getJsonRawBody() Unfortunately it is not clear if getJsonRawBody() returns associative arrays or stdObjects when decoding an object. (The PHP native function json_decode() has a flag to specify which one you would like) Even worse, I got an associative array in one case and a stdObject in another.

So what is the rule here?



125.8k
Accepted
answer

Apparently getJsonRawBody() has a $assoc parameter which, I assume, converts the JSON into an associative array, regardless of what the JSON actually is.

I wonder if perhaps Angular is sending an associative array as JSON some times, and an object other times. I believe JSON (which, remember, is not strictly Javascript) allows for string keys in arrays. If you're curious, try the getRawBody() method - just to see what the actual content of the request is. Maybe Phalcon is accurately converting what Angular is sending.

I did not find the $assoc parameter in the documentation. Thanks for pointing it out.

I came to the same conclusion as Quasipickle, and found a few JSON objects with quoted keys which are decoded as associative arrays. Thanks for helping to solve this.

Yeah - I only found out about it because when I searched, there was a GitHub thread about the fact that the parameter isn't documented.