0

I want to retrieve name from the following JSON object:

enter image description here

In my AJAX response function I have the following:

success: function (response) {
    .each(response, function (idx, obj) {
         console.log(obj[1]);
         var name = obj.author["name"];
         generatedHtml.push(`${name} <br><br>`);
    });
},

I keep getting the error obj.author is undefined. What should I do?

EDIT: The complete JSON is available here: https://gist.github.com/SeloSlav/acb223dd25c589c660c7326dbf3e7bdc

Martin Erlic
  • 5,467
  • 22
  • 81
  • 153
  • JSON is a *textual notation* for data exchange. [(More here.)](http://stackoverflow.com/a/2904181/157247) If you're dealing with JavaScript source code, and not dealing with a *string*, you're not dealing with JSON. – T.J. Crowder Jun 05 '18 at 08:30
  • `obj.author` is an array so you need to access it by index, eg: `obj.author[11].value // = 'Yart'`. Also note that having multiple objects holding one property in an array is very odd. If you can convert the response format I'd suggest holding each property within a single object – Rory McCrossan Jun 05 '18 at 08:31
  • @RoryMcCrossan - I don't think `obj.author` is an array. I think `obj[1].key` is the string `"author"`. Martin - Perhaps you're looking for `obj[1].value`? That's an array of objects, again with properties called `key` and `value`. If you can't rely on it being at index 1, you'll want to use `find` to find the entry with `key == "author"`. It's hard to say without a clearer idea what `obj` is. Perhaps you could show the result of `console.log(JSON.stringify(obj, null, 2))` so we don't have to worry about what your console is doing to decorate things. – T.J. Crowder Jun 05 '18 at 08:32
  • What is `obj.value[4].value` – Amarghosh Jun 05 '18 at 08:32
  • Yeah, you're right on second glance. It seems `obj.value[11].value` is the correct way. The structure of this response seems to be, frankly, a complete mess. – Rory McCrossan Jun 05 '18 at 08:33
  • @Amarghosh ``obj.value[4].value`` is undefined. – Martin Erlic Jun 05 '18 at 08:33
  • @MartinErlic could you please post the RAW JSON response. – Rory McCrossan Jun 05 '18 at 08:34
  • @RoryMcCrossan - Yeah, arrays of `{key, value}` pairs seem a bit odd in an environment with dynamic object properties. :-) Unless `key` can be repeated or something... – T.J. Crowder Jun 05 '18 at 08:34

1 Answers1

1

Although you probably need to alter your JSON file, in your current state you need to iterate over all the key/value pairs and find the one with key being author, then do the same on its own value (which is again a list of key/value pairs) and search for a pair whose key is name

$.ajax({
  url: 'https://gist.githubusercontent.com/SeloSlav/acb223dd25c589c660c7326dbf3e7bdc/raw/832a489c6eeb87913712862e0798a13c1c62b161/gistfile1.txt',
  dataType: 'json',
  success: function(response) {
    $.each(response, function(idx, obj) {
      var name,
          author = obj.find(function(node) {return node.key === 'author'});
      if (author) {
        name = author.value.find(function(node) {return node.key === 'name'});

        if (name) {
          console.log(name.value);
          //generatedHtml.push(`${name.value} <br><br>`);
        }
      }
    });
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317