How to access json data item under dynamic key?

Question

I am trying to access the "description" in the json available at this url: https://librivox.org/api/feed/audiobooks/id/11591/extended/1/format/json in a PhantomJS script. My script just has one step:

steps = [

        /*
         * Step 1 - print out description
         */
        function(){
                page.open( 'https://librivox.org/api/feed/audiobooks/id/11591/extended/1/format/json' , function () {
                        var jsonSource = page.plainText;
                        //console.log( jsonSource );
                        var resultObject = JSON.parse(jsonSource);
                        var bookkey = Object.keys(resultObject.books);
                        console.log( bookkey );
                        console.log( resultObject.books.bookkey );
                });
        },
];

When I run my script I get this output:

14
undefined

I am not sure how I am suppose to access the description value.

Here is the json that comes from the URL mentioned above:

{"books":{"14":{"id":"11591","title":"Soldier Of The Legion; <br>An Englishman's Adventures Under The French Flag in Algeria And Tonquin","description":"An educated gentleman, Mr Manington has given an insight into the unusual experiences of an Englishman in the French Foreign Legion, such as no ordinary \"mercenary\" could have done. Most of the narrative deals with Tonquin, and the fighting there against the rebels in their forest fastnesses. Incidentally, in giving an account of his friendship for the native sergeant, Doy-Tho, the author has been able to impart to the pages of the book an Oriental atmosphere that we think will prove attractive to the reader. - Summary by Editors' Note","url_text_source":"http:\/\/www.gutenberg.org\/ebooks\/53902","language":"English","copyright_year":"1907","num_sections":"15","url_rss":"http:\/\/librivox.org\/rss\/11591","url_zip_file":"http:\/\/www.archive.org\/download\/\/a_soldier_of_the_legion_1702_librivox\/a_soldier_of_the_legion_1702_librivox_64kb_mp3.zip","url_project":"","url_librivox":"http:\/\/librivox.org\/a-soldier-of-the-legion-an-englishmans-adventures-under-the-french-flag-in-algeria-and-tonquin-by-george-manington\/","url_other":null,"totaltime":"07:56:47","totaltimesecs":28607,"authors":[{"id":"12503","first_name":"George","last_name":"Manington","dob":"1866","dod":"?"}],"url_iarchive":"http:\/\/archive.org\/details\/a_soldier_of_the_legion_1702_librivox","sections":[{"id":"392478","section_number":"0","title":"Dedication, Preface, Editors' Note","listen_url":"https:\/\/librivox.org\/uploads\/aradlaw\/soldierlegion_00_manington_128kb.mp3","language":"English","playtime":"316","file_name":"soldierlegion_00_manington_128kb.mp3","readers":[{"reader_id":"6454","display_name":"David Wales"}]},{"id":"392479","section_number":"1","title":"Chapter 1  Ministere de la Guerre, The Recruiting Office, etc","listen_url":"https:\/\/librivox.org\/uploads\/aradlaw\/soldierlegion_01_manington_128kb.mp3","language":"English","playtime":"3049","file_name":"soldierlegion_01_manington_128kb.mp3","readers":[{"reader_id":"6454","display_name":"David Wales"}]},{"id":"392480","section_number":"2","title":"Chapter 2 General Inspection, The Band Of The Legion, etc","listen_url":"https:\/\/librivox.org\/uploads\/aradlaw\/soldierlegion_02_manington_128kb.mp3","language":"English","playtime":"1923","file_name":"soldierlegion_02_manington_128kb.mp3","readers":[{"reader_id":"6454","display_name":"David Wales"}]},{"id":"392481","section_number":"3","title":"Chapter 3 Part 1  Some Information Concerning Tonquin, Haiphong, etc","listen_url":"https:\/\/librivox.org\/uploads\/aradlaw\/soldierlegion_03_manington_128kb.mp3","language":"English","playtime":"2561","file_name":"soldierlegion_03_manington_128kb.mp3","readers":[{"reader_id":"6454","display_name":"David Wales"}]},{"id":"392819","section_number":"4","title":"Chapter 3 Part 2","listen_url":"https:\/\/librivox.org\/uploads\/aradlaw\/soldierlegion_04_manington_128kb.mp3","language":"English","playtime":"2495","file_name":"soldierlegion_04_manington_128kb.mp3","readers":[{"reader_id":"6454","display_name":"David Wales"}]},{"id":"392482","section_number":"5","title":"Chapter 4 Part 1 The Difficulties Of Obtaining Military Intelligence, Native spies, etc","listen_url":"https:\/\/librivox.org\/uploads\/aradlaw\/soldierlegion_05_manington_128kb.mp3","language":"English","playtime":"2171","file_name":"soldierlegion_05_manington_128kb.mp3","readers":[{"reader_id":"6454","display_name":"David Wales"}]},{"id":"394973","section_number":"6","title":"Chapter 4 Part 2","listen_url":"https:\/\/librivox.org\/uploads\/aradlaw\/soldierlegion_06_manington_128kb.mp3","language":"English","playtime":"2374","file_name":"soldierlegion_06_manington_128kb.mp3","readers":[{"reader_id":"6454","display_name":"David Wales"}]},{"id":"392483","section_number":"7","title":"Chapter 5 Part 1 An execution, A Rebel Chieftan, etc","listen_url":"https:\/\/librivox.org\/uploads\/aradlaw\/soldierlegion_07_manington_128kb.mp3","language":"English","playtime":"1860","file_name":"soldierlegion_07_manington_128kb.mp3","readers":[{"reader_id":"6454","display_name":"David Wales"}]},{"id":"395106","section_number":"8","title":"Chapter 5 Part 2","listen_url":"https:\/\/librivox.org\/uploads\/aradlaw\/soldierlegion_08_manington_128kb.mp3","language":"English","playtime":"1835","file_name":"soldierlegion_08_manington_128kb.mp3","readers":[{"reader_id":"6454","display_name":"David Wales"}]},{"id":"392484","section_number":"9","title":"Chapter 6 Part 1 La Soeur Agnes, Exeat, etc","listen_url":"https:\/\/librivox.org\/uploads\/aradlaw\/soldierlegion_09_manington_128kb.mp3","language":"English","playtime":"2108","file_name":"soldierlegion_09_manington_128kb.mp3","readers":[{"reader_id":"6454","display_name":"David Wales"}]},{"id":"395107","section_number":"10","title":"Chapter 6 Part 2","listen_url":"https:\/\/librivox.org\/uploads\/aradlaw\/soldierlegion_10__manington_128kb.mp3","language":"English","playtime":"2007","file_name":"soldierlegion_10__manington_128kb.mp3","readers":[{"reader_id":"6454","display_name":"David Wales"}]},{"id":"392485","section_number":"11","title":"Chapter 7 Part 1 The Last Struggles Of A Rebellion, Departure Of Captain Plessier, etc","listen_url":"https:\/\/librivox.org\/uploads\/aradlaw\/soldierlegion_11_manington_128kb.mp3","language":"English","playtime":"1611","file_name":"soldierlegion_11_manington_128kb.mp3","readers":[{"reader_id":"6454","display_name":"David Wales"}]},{"id":"395159","section_number":"12","title":"Chapter 7 Part 2","listen_url":"https:\/\/librivox.org\/uploads\/aradlaw\/soldierlegion_12_maningon_128kb.mp3","language":"English","playtime":"1306","file_name":"soldierlegion_12_maningon_128kb.mp3","readers":[{"reader_id":"6454","display_name":"David Wales"}]},{"id":"392507","section_number":"13","title":"Chapter 8 Part 1 General Voyron, Organisation Of The Brigade, etc","listen_url":"https:\/\/librivox.org\/uploads\/aradlaw\/soldierlegion_13_manington_128kb.mp3","language":"English","playtime":"1673","file_name":"soldierlegion_13_manington_128kb.mp3","readers":[{"reader_id":"6454","display_name":"David Wales"}]},{"id":"395160","section_number":"14","title":"Chapter 8 Part 2","listen_url":"https:\/\/librivox.org\/uploads\/aradlaw\/soldierlegion_14_manington_128kb.mp3","language":"English","playtime":"1318","file_name":"soldierlegion_14_manington_128kb.mp3","readers":[{"reader_id":"6454","display_name":"David Wales"}]}],"genres":[{"id":"73","name":"War & Military"},{"id":"108","name":"Travel & Geography"}],"translators":[]}}}

Show source
| javascript   | json   | phantomjs   2017-03-13 23:03 1 Answers

Answers ( 1 )

  1. 2017-03-13 23:03

    Moving from comment to answer.

    Try: resultObject.books[bookkey[0]]

    This should allow you to then call resultObject.books[bookkey[0]].description.

    Enjoy.

    NOTE

    See example: https://jsfiddle.net/Twisty/fL129dnz/ & https://jsfiddle.net/Twisty/fL129dnz/3/

    Objects.keys() returns an array of keys. It appears some browsers may accept an array containing a single element as an Index. For this case, since only 1 book is being called up, this works: resultObject.books[bookkey]. To be more portable, I advise being cautious and defining a single result: resultObject.books[bookkey[0]]

    Thanks goes to @torazaburo for pointing this out.

◀ Go back