Vinisha
Vinisha

Reputation: 13

extract specific part of API response to JSON object in Javascript

I am trying to interrogate an API response from the Recognize (fashion recognition) API. The data is returned as set out below. I am trying to extract the items of attire from the following object.

Object {data: "   Array↵(↵    [id] => 1309↵)↵{"Status":true,"Data":{"VufindTags":["Dress"," Purse"]}}", status: 200, headers: function, config: Object, statusText: "OK"}config: Objectdata: "   Array↵(↵    [id] => 1309↵)↵{"Status":true,"Data":{"VufindTags":["Dress"," Purse"]}}"headers: function (name) {status: 200statusText: "OK"__proto__: Object

I have tried to access using data.data which returned the following as a string:

"   Array
(
[id] => 1309
)
{"Status":true,"Data":{"VufindTags":["Dress"," Purse"]}}"

I then tried to use JSON.parse to extract the data from the VufindTags. That did not work.

Is there a way to convert this into a JSON Object??

Thanks for any help!!

Upvotes: 0

Views: 451

Answers (1)

Curtis Autery
Curtis Autery

Reputation: 76

It looks like the vufind API is giving you PHP print_r output instead of JSON. The best thing to do would be to get them to fix their API. Failing that, you can pull the JSON-ified bits out. I had some success with this:

myObj = JSON.parse(apiOutput.slice(apiOutput.indexOf('{')))

...but I wouldn't put that into an app and call it production ready, especially when the API clearly isn't giving you what it should in the first place.

Upvotes: 1

Related Questions