0

I have this URL which returns me a JSON value, my task is to access 3 values out of it that is name, title and ISBN.

Since the content is heavily nested, I am not able to access the values inside it.

All the tutorials I have referred so far, have simple nesting in them

I have attached my code that I have written so far:

$(document).ready(function() {
  var globalJsonVar;
  alert("check");
  $.getJSON("https://openlibrary.org/api/books?bibkeys=ISBN:9781606868829&jscmd=details&format=json", function(result) {
    if (Object.keys(result).length > 0) {
      alert("conetnt is present");
      console.log(result);
      alert(Object.values(result));
      alert(result.ISBN: 9781606868829. bib_key); // my attempt at accessing the values
    } else {
      alert("hoax");
    }
  });
});

The JSON content that I am receiving is

{
  "ISBN:9780143039648": {
    "bib_key": "ISBN:9780143039648",
    "preview": "noview",
    "preview_url": "https://openlibrary.org/books/OL17924003M/The_guide",
    "info_url": "https://openlibrary.org/books/OL17924003M/The_guide",
    "details": {
      "number_of_pages": 196,
      "series": [
        "Penguin classics"
      ],
      "lc_classifications": [
        "PR9499.3.N3 G85 2006"
      ],
      "latest_revision": 7,
      "uri_descriptions": [
        "Contributor biographical information",
        "Publisher description"
      ],
      "genres": [
        "Fiction."
      ],
      "title": "The guide",
      "languages": [{
        "key": "/languages/eng"
      }],
      "subjects": [
        "Malgudi (India : Imaginary place) -- Fiction.",
        "Tour guides (Persons) -- Fiction.",
        "Bharata natyam dancers -- Fiction.",
        "Spiritual life -- Hinduism -- Fiction.",
        "India -- Fiction."
      ],
      "publish_country": "nyu",
      "by_statement": "R.K. Narayan ; introduction by Michael Gorra.",
      "type": {
        "key": "/type/edition"
      },
      "uris": [
        "http://www.loc.gov/catdir/enhancements/fy0716/2006044314-b.html",
        "http://www.loc.gov/catdir/enhancements/fy0716/2006044314-d.html"
      ],
      "revision": 7,
      "publishers": [
        "Penguin Books"
      ],
      "dewey_decimal_class": [
        "823/.914"
      ],
      "last_modified": {
        "type": "/type/datetime",
        "value": "2012-08-05T21:10:58.887826"
      },
      "key": "/books/OL17924003M",
      "authors": [{
        "name": "Rasipuram Krishnaswamy Narayan",
        "key": "/authors/OL5911201A"
      }],
      "publish_places": [
        "New York"
      ],
      "oclc_number": [
        "65644730"
      ],
      "pagination": "xxiv, 196 p. ;",
      "created": {
        "type": "/type/datetime",
        "value": "2008-10-07T19:30:19.584308"
      },
      "url": [
        "http://www.loc.gov/catdir/enhancements/fy0716/2006044314-b.html",
        "http://www.loc.gov/catdir/enhancements/fy0716/2006044314-d.html"
      ],
      "notes": {
        "type": "/type/text",
        "value": "Originally published: New York : Viking Press, 1958.\n\nIncludes bibliographical references (p. [xxi]-xxii)."
      },
      "identifiers": {
        "goodreads": [
          "129877"
        ],
        "librarything": [
          "6930"
        ]
      },
      "isbn_13": [
        "9780143039648"
      ],
      "lccn": [
        "2006044314"
      ],
      "isbn_10": [
        "0143039644"
      ],
      "publish_date": "2006",
      "works": [{
        "key": "/works/OL1057183W"
      }]
    }
  }
}
mplungjan
  • 169,008
  • 28
  • 173
  • 236
confused_geek
  • 249
  • 1
  • 14
  • 3
    Try with `result["ISBN:9781606868829"].bib_key` – Satpal Oct 31 '17 at 13:40
  • You already have the ISBN as you make the request with it, and `title` and `name` appear to be the same thing...? – Rory McCrossan Oct 31 '17 at 13:41
  • You can use the 'dot operator` (`.`) for named values and the square bracket with numbers for items in an array. So `console.log` accesses the function at `log` inside the object named `console`. `fruits[0]` would return the first item in the array fruits. You can also put strings/variables between brackets-- so `console["log"]` would also work. – Alexander Nied Oct 31 '17 at 13:41
  • I inserted this inside the alert statement , and i got the same reply , i.e, `ISBN:9781606868829` – confused_geek Oct 31 '17 at 13:43
  • Please use 4 space tabbing in all posts to achieve code clarity and minimize horizontal scrolling. – mickmackusa Oct 31 '17 at 13:43
  • @mickmackusa ... what's wrong with a 2 space indentation?! – lumio Oct 31 '17 at 13:44
  • 2
    @mickmackusa SO uses the Google code tidier library which uses 2 spaces for tabs to fill as much code as possible in the horizontal space. – Rory McCrossan Oct 31 '17 at 13:45
  • @HarjeevSingh `result['ISBN:9780143039648'].bib_key` contains the ISBN number. – lumio Oct 31 '17 at 13:46
  • @mickmackusa - a simple TIDY before saving a snippet and then removal of the top line to not make it a snippet was enough. We do not want no filthy tabs – mplungjan Oct 31 '17 at 13:46
  • My comment, from the review queue, was posted while the code had ~20 space indentation. I don't mind 2 space indentation. – mickmackusa Oct 31 '17 at 13:47
  • @lumio . Thank you . i am able to access title now . going to access author now. – confused_geek Oct 31 '17 at 13:52

2 Answers2

-1

You have to use [] if you want to have access to keys that have special chars, for example result['something-with:special-chars']

Sylwek
  • 856
  • 1
  • 9
  • 24
-1
result.ISBN:9781606868829.bib_key

is a syntax error. : is not allowed here. You want to use the following syntay :

result["ISBN:9781606868829"].bib_key
Jeremy Thille
  • 26,047
  • 12
  • 43
  • 63
  • 1
    Should you not look for dupe and close it? – Rajesh Oct 31 '17 at 13:42
  • 1
    So now we get downvotes when posting good answers. I'm wondering how to make some rep on SO nowadays. Everything is a duplicate, and when we answer it anyways, we get downvoted. Why didn't _you_ vote to close it instead? – Jeremy Thille Oct 31 '17 at 13:44
  • i tried this approach . it did'nt work out – confused_geek Oct 31 '17 at 13:48
  • @JeremyThille i already voted to close but net seems to be intermittent. Also point is, we should try to reduce content duplications. So for that, we should not answer basic questions. You will find questions worth answering, but gaining rep from answering same thing 10 times will not do any good. Rep does not count much in real world. It's knowledge that is important – Rajesh Oct 31 '17 at 13:49
  • Oh I totally agree, I'm just trying to reach 10k :) For the fun – Jeremy Thille Oct 31 '17 at 15:43