What is the best way to retrieve a specific JSON data item that is stored under a

After attempting to access the "description" from the json data at the following URL: using a PhantomJS script, I encountered a specific issue. Below is a snippet of the script I used:

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 );
                });
        },
];

Upon running the script, the output I received was as follows:

14
undefined

My challenge lies in figuring out how to access the description value from the JSON data. Here is a snippet of the JSON content retrieved from the mentioned URL:

{"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...

Answer №1

Transitioning from a comment to an answer.

Consider using: resultObject.books[bookkey[0]]

This code should enable you to then access

resultObject.books[bookkey[0]].description
.

Enjoy exploring this solution.

IMPORTANT

Check out this example: https://jsfiddle.net/Twisty/fL129dnz/ & https://jsfiddle.net/Twisty/fL129dnz/3/

Objects.keys() gives back an array of keys. Some browsers might recognize an array with a single element as an index. In this particular scenario where only one book is requested, this code can work: resultObject.books[bookkey]. For better compatibility, it's recommended to be careful and define a single result: resultObject.books[bookkey[0]]

Credit to @torazaburo for bringing this to light.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having trouble setting the InnerHTML property of Null on my Ionic app, what could be the issue?

I'm working on a code to display the remaining time for generating a random code in the DOM. var count = setInterval(function () { var date = new Date(); var currentSecond = date.getSeconds(); ...

Why doesn't package.json typically utilize the preset values stored in the .npmrc file?

Windows 10 x64 Based on the information found here, I created a file called C:\Users\bushm\.npmrc with the following content: author = "Andrey Bushman" However, when I run the command npm init -y in a new directory, I noticed that the pac ...

The combination of a modal box, checkbox, and cookie feature creates

I am trying to accomplish the following tasks: When the homepage loads, I want a modal box to appear Inside the modal box, there should be a form with a mandatory checkbox After checking the checkbox, submit the form and close the modal box to return to ...

Analyzing JSON data and displaying the information on the console

I received a JSON with the structure shown below - { "gridDefinition": {}, "zoneDefinitions": [ { "status": "Pending", "name": "xxx-1", "globalName": "xxx-2", "id": 10, "memory": ...

Tips for inserting an element into every tier of a multi-layered array

I am faced with a challenging task of assigning an id field to objects within an array that has infinite levels. The rule is simple - for every level, the ID should correspond to the level number starting from 1. { "name": "Anything2&quo ...

How can I create a custom validator in Angular 2 that trims the input fields?

As a newcomer to Angular, I am looking to create a custom validator that can trim the input field of a model-driven approach form. However, I have encountered difficulties during implementation. When attempting to set the value using setValue() within th ...

What is the most effective method in React JS to achieve real-time results with every keystroke by making REST calls to the server?

We are dealing with a huge database and the task at hand is to display results as the user types them in the search box. Preloading queries to attempt matching is out of question due to the enormity of our data. Currently, we make a server request with th ...

Adjust the CSS property of a div to have a fixed position when scrolling

I attempted to implement a fixed div on scroll in Vue.js using the following code: ... async created() { window.addEventListener('scroll', this.calendarScroll); } methods: { calendarScroll() { console.log('Scroll'); cons ...

Including an additional row in a pre-existing table

My goal is to dynamically add a new row when a button is clicked, right after the row containing the button. To achieve this, I'm using jQuery (version 1.10.2) to handle the creation of new rows. The issue I'm facing is related to the behavior o ...

What could be causing my dropdown links to malfunction on the desktop version?

I've been developing a responsive website and encountering an issue. In desktop view, the icon on the far right (known as "dropdown-btn") is supposed to activate a dropdown menu with contact links. However, for some unknown reason, the links are not f ...

Retrieve a specific subset of keys from a JSON column

I am working with a table column that looks like this: https://i.sstatic.net/5ewma.png The goal is to extract the trailing numbers from the key names and organize them as shown below: ID 1 2 3 4 Currently, I am able to retrieve it using the ...

Dropbox menu within an extended webpage

I am looking to create a dropdown menu that behaves like the one on this website. The goal is for the dropdown to cover the entire webpage, hide the scroll bar, and "unmount" the other elements of the page, while still displaying them during the transition ...

Engaging User Forms for Enhanced Interactivity

I'm in the process of developing an application that involves filling out multiple forms in a sequential chain. Do you have any suggestions for creating a more efficient wizard or form system, aside from using bootstrap modals like I currently am? C ...

Converting a lengthy HTML document into numerous JPEGs (or any preferred image format)

I am working on a lengthy webpage that can be viewed as having 40 individual pages. Each "page" is separated by a horizontal rule () and has a fixed height of 860px. My goal is to convert each of these "pages" into JPG images automatically. Can anyone pr ...

Is it possible to dynamically add and remove items using React state?

I am currently working on implementing a dynamic queue of game players in my React state. The goal is to avoid hardcoding the number of players who can participate and achieve this state update using a JS FIFO queue. My objective is to create a player que ...

Creating a Commitment - Resolving the "Expected ')' after argument list" Error

Can someone please help me locate the syntax error in this code snippet? I've been searching for ages! An error occurred: Uncaught SyntaxError: missing ) after argument list promiseArray.push( new Promise(function (resolve, reject) { ...

Tips for using an array as a jQuery selector in JavaScript

Managing an element with an array id id="x[]" can be tricky when it comes to dynamically changing content based on database entries. This specific element is essentially a delete button for removing table rows from the database. <div align="center" id= ...

Does implementing a product listing with filter, sorting, and search options through Ajax violate any REST principles?

As I work on creating a product listing library for a web application, it's crucial to incorporate filter, search, and sort functionalities. A web service is available that can fetch results based on these parameters, including page number and product ...

Decoding a JSONArray of nested JSONArrays in Java

I'm struggling with parsing a JSONArray in this format: { "details": { "rows_count": 4, //maximum is 4 "analysis": [ [ [ "BEACH", 59.35 ], [ "OCEAN", 40.65 ] ],//row1 , could have more than 2 arrays [ [ "OCEAN", 70.2 ], [ "BEA ...

Ways to utilize jquery to limit the length of an editable div and prevent it from exceeding our specified restriction

Imagine a scenario where you have the following code snippet: <div id="editing" contenteditable onclick="document.execCommand('selectAll',false,null)">Put text here...</div> In this situation, let's say you want to impose a r ...