1

i have built a custom module to get Faqs in a REST API.

but the json format not return as what i need!

Expected Result:

   {
    {
        "faq_id": "1",
        "category_id": "1",
        "question": "What is Lorem Ipsum?",
        "answer": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen boo",
        "sort_order": "0",
        "status": "1"
    },
    {
        "faq_id": "2",
        "category_id": "1",
        "question": "Why do we use it?",
        "answer": "It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters",
        "sort_order": "0",
        "status": "1"
    }
}

Current Result:

[
    {
        "faq_id": "1",
        "category_id": "1",
        "question": "What is Lorem Ipsum?",
        "answer": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen boo",
        "sort_order": "0",
        "status": "1"
    },
    {
        "faq_id": "2",
        "category_id": "1",
        "question": "Why do we use it?",
        "answer": "It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters",
        "sort_order": "0",
        "status": "1"
    }
]

the issue is in this "["

my code: API Model:

/**
     * {@inheritdoc}
     */
    public function getFaqs()
    {
        $collections = $this->collectionFactory->create()->addFieldToFilter('status', ['eq' => 1]);;
        $result = $collections->toArray();
        return $result['items'];
    }

interface:

interface Faqsinterface
{
  /**
     * GET for Post api
     * @param mixed
     * @return array
     */
    
    public function getFaqs();
}

kindly advise, what should i do to solve it?

1 Answer 1

1

The JSON representation of an array or a list is the '[ ]' pair.

Due to the fact that you explicitly used $collections->toArray(), the square brackets are used in the JSON.

In order to obtain curly brackets, you will have to return an object with pairs of keys and values, BUT this means you won't have a proper way to determine different items from one another and this is not a good representation for a list/ array of items.

5
  • Thank Diana, could you please advise about what should i do? cuz i should use ->toArray() to get the items list... is there another way?
    – Medomage
    Commented Aug 23, 2021 at 12:41
  • I would say you leave it this way and on the other end (where you are processing the resulted JSON) you make the code 'understand' the '[ ]' as it is a list of items.
    – Diana
    Commented Aug 24, 2021 at 8:07
  • 1
    actually this is request by the Mobile Developer and he require the format {{}}, i'll try to find a way,, thanks
    – Medomage
    Commented Aug 24, 2021 at 8:08
  • @Medomage : Did you get any solution ?
    – Krupali
    Commented Apr 28, 2022 at 9:56
  • 1
    @Krupali yes, just print_r(json_encode($response)); die; and set Headers as application/json.
    – Medomage
    Commented Jun 12, 2022 at 6:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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