I am getting rest API response as like below,

"field1" : "val1",
"field1" : "val2",

How do I get API response as object, without braces []. It will return only one object every time, so there is no need of array.

"field1" : "val1",
"field1" : "val2",
  • Did you ever figure this out? I am dealing with the same issue. It works, but it's just annoying. I would prefer to consume a json object rather than an array containing the object. Apr 26, 2018 at 18:19
  • I done by overriding webapi controller May 8, 2018 at 14:11
  • Have you got solution? We are looking for same solutions
    – jack
    Aug 2, 2018 at 9:56

1 Answer 1


Magento 2 Api returns json type (but it is possible to get xml instead of json).
You can't change it "like that" to object.

If you are using PHP to connect to M2 Api use json_decode(string $json) PHP function to decode json.
You can use also json_encode($value) to create json.

  • I can see Magento default api's responses are returning as Object instead of Array(Object) Dec 11, 2017 at 7:08

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.