-2

I am iterating through an array with an forEach loop and each time I get an JSON object and each time I want to get certain values from the JSON object but somehow this does not work. console.log(json.responseJSON) always returns undefined.

The code looks like this:

searchterms = ["a", "b","c"];
searchterms.forEach(function(entry) {

    console.log(entry);

    json = $.getJSON('https://www.markerapi.com/api/v1/trademark/searchall/'+entry+'/wordcount/2/limit/20/username/XXX/password/XXX')

    console.log(json);

    console.log(json["responseJSON"])
});

The JSON for the first item in the array looks like this:

responseJSON: {"count":1,"trademarks":[{"serialnumber":"71045585","wordmark":"A","code":"GS0371","description":"WRITING AND PRINTING PAPER","registrationdate":"04\/08\/1913"}]}"

Later on I planned to get the "wordmark" and the "description" value from the JSON. How would I do this?

Thank you for any help!

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
  • 2
    [`$.getJson`](http://api.jquery.com/jquery.getjson/) doesn't return the result. You'll have to use the `success` callback or the `complete` method to retrieve the resulting JSON. – H77 Mar 07 '18 at 08:16
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Mohammad Usman Mar 07 '18 at 08:42

1 Answers1

0

check the documentation of jquery you need to place callback for getJSON. Check below code snippet. It may help you.

searchterms = ["a", "b", "c"];
searchterms.forEach(function(entry) {
  console.log(entry);
  json = $.getJSON('https://www.markerapi.com/api/v1/trademark/searchall/' + entry + '/wordcount/2/limit/20/username/XXX/password/XXX', function(data) {
    console.log(data);
    console.log(data["responseJSON"])
  });
});
swaroop pallapothu
  • 588
  • 1
  • 6
  • 15