0

I'm new to Angular and having trouble trying to parse a JSON response from this API: http://icd10api.com/?s=C50&desc=long&r=json

in this codepen here: http://codepen.io/FritzAPI/pen/dNZWKZ

which I got working with a hardcoded modified response:

function loadVegetables() {
    var veggies = [
        {"Name":"C50","Description":"Malignant neoplasm of breast"},{"Name":"C50.0","Description":"Malignant neoplasm of nipple and areola"},{"Name":"C50.01","Description":"Malignant neoplasm of nipple and areola, female"},{"Name":"C50.011","Description":"Malignant neoplasm of nipple and areola, right female breast"},{"Name":"C50.012","Description":"Malignant neoplasm of nipple and areola, left female breast"},{"Name":"C50.019","Description":"Malignant neoplasm of nipple and areola, unspecified female breast"},{"Name":"C50.02","Description":"Malignant neoplasm of nipple and areola, male"},{"Name":"C50.021","Description":"Malignant neoplasm of nipple and areola, right male breast"},{"Name":"C50.022","Description":"Malignant neoplasm of nipple and areola, left male breast"},{"Name":"C50.029","Description":"Malignant neoplasm of nipple and areola, unspecified male breast"}
    ];

    return veggies.map(function (veg) {
        veg._lowername = veg.Name.toLowerCase();
        veg._lowertype = veg.Description.toLowerCase();
        return veg;
    });
}

but as soon as the response is wrapped in a search array {"Search":[...]} this function no longer works.

1
  • Please provide all relevant code in an minimal reproducible example in the question itself, not only on a third-party site. Commented May 9, 2017 at 17:38

2 Answers 2

1

It seems like your hardcoded object is just slightly different than the response from the API. The API returns an object with a Key Search. The value of Search is what your veggies array is imitating. So something like:

apiResponse.Search.map(function (veg) {
  veg._lowername = veg.Name.toLowerCase();
  veg._lowertype = veg.Description.toLowerCase();
  return veg;
});
Sign up to request clarification or add additional context in comments.

5 Comments

This is returning a "TypeError: Cannot read property 'map' of undefined... codepen.io/FritzAPI/pen/LyQYOb
Took a stab in the dark and this seems to be working: return veggies[0].Search.map
Interesting. The response from the API is just an object. Somehow you have an array of objects. How are you building up the veggies variable?
The end goal is to actually get the response from the API and not work with this hardcoded variable.
I was trying to understand the parsing before attempting that part, that's why I was working with this hardcoded variable that matches the API response.
0

DEMO

var jsonObj = {"Search":[{"Name":"C50","Description":"Malignant neoplasm of breast"},{"Name":"C50.0","Description":"Malignant neoplasm of nipple and areola"},{"Name":"C50.01","Description":"Malignant neoplasm of nipple and areola, female"},{"Name":"C50.011","Description":"Malignant neoplasm of nipple and areola, right female breast"},{"Name":"C50.012","Description":"Malignant neoplasm of nipple and areola, left female breast"},{"Name":"C50.019","Description":"Malignant neoplasm of nipple and areola, unspecified female breast"},{"Name":"C50.02","Description":"Malignant neoplasm of nipple and areola, male"},{"Name":"C50.021","Description":"Malignant neoplasm of nipple and areola, right male breast"},{"Name":"C50.022","Description":"Malignant neoplasm of nipple and areola, left male breast"},{"Name":"C50.029","Description":"Malignant neoplasm of nipple and areola, unspecified male breast"}],"totalResults":"82","Response":"True"};

var res = jsonObj.Search.map(item => { 
  item._lowername = item.Name.toLowerCase();
  item._lowertype = item.Description.toLowerCase();
  return item;
});

console.log(res);

2 Comments

I still had to add the specific array index "veggies[0]" to get this to work: codepen.io/FritzAPI/pen/LyQYOb
Yes because the json provided in plnkr is an array of objects [{..}] but here in OP it is only object {}.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.