This is my first post so please let me know if I missed sharing any detail on my problem.

I have below files ready in Magento kept in app/code/community:

A. Autogen > Restapi > etc > api2.xml

B. Autogen > Restapi > etc > config.xml

C. Autogen > Restapi > Model > Api2 > Order.php

D. Autogen > Restapi > Model > Api2 > Order > Rest > Admin > V1.php

Below is the part of code written in V1.php:

protected function _retrieveCollection() {
    $return_variable = array();
    $page = $this->getRequest()->getParam('page');
    $limit = $this->getRequest()->getParam('limit');

    $collection = Mage::getModel('sales/order')->getCollection()

    $return_variable['order_count'] = $collection->getSize();
    $order_counter = 0;
    foreach ($collection as $order) {
        $return_variable['order'][$order_counter]['order_id'] = $order->getId();
        $return_variable['order'][$order_counter]['increment_id'] = $order->getIncrementId();

        $order_counter = $order_counter + 1;

    return json_encode($return_variable);   //I will refer this line again

When I hit restapi with Restclient, I get below output which is not fit for json_decode:


When I change (See commented line in above code) from "return json_encode($return_variable);" to "return $return_variable;", I get below output. This response is JSON fit for decode but I should receive more key-value pair instead of just 1:


Please help me to get JSON fit for decode data with all key-value pair.

Your help is appreciated.

Magento Version 1.8

2 Answers 2


Try to change your foreach to something like this.

    foreach ($collection as $order) {
        $return_variable['order'][] = [
            'order_id' => $order->getId(),
            'increment_id' => $order->increment_id(),
  • just tried your solution. Result is same as earlier.
    – Ashish
    Aug 8, 2019 at 9:36
  • Remove cur page and page size limit and try again.
    – Tsita
    Aug 8, 2019 at 9:40
  • tried this as well. same problem.
    – Ashish
    Aug 8, 2019 at 9:43
  • Hmm i don't know, try to pass a second parameter in json_encode this constant JSON_UNESCAPED_SLASHES
    – Tsita
    Aug 8, 2019 at 9:51
  • nope. I think \ is added because of magento at the time of giving response
    – Ashish
    Aug 8, 2019 at 9:59

Finally I gave-up on getting correct JSON format. I used str_replace to remove un-necessary charactors to move forward.

Hope this helps anyone.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.