What is the best way to handle an API that consistently returns identical values but varying responses?

(Apologies if the title is unclear, I struggled to phrase it accurately)

I am attempting to retrieve a response from an API, however, the API is returning multiple responses with the same name. Example of a response:

{
    "xuid": 2535436668322645,
    "state": "Online",
    "devices": [
        {
            "type": "XboxOne",
            "titles": [
                {
                    "id": 714681658,
                    "name": "Home",
                    "placement": "Background",
                    "state": "Active",
                    "lastModified": "2016-11-22T23:45:08.8296994Z"
                },
                {
                    "id": 74304278,
                    "activity": {
                        "richPresence": "Lvl 30 in Hudson Yards"
                    },
                    "name": "Tom Clancy's The Division",
                    "placement": "Full",
                    "state": "Active",
                    "lastModified": "2016-11-22T23:45:08.8296994Z"
                }
            ]
        }
    ]
}

My question is, how can I extract the ID from the "second" response using JavaScript? (Referring to the one with "Tom Clancy's The Division" in the name) Thanks!

Answer №1

Here's a possible solution to guide you in the right direction (not yet verified)

let data = JSON.parse(YOUR_JSON_DATA);
data->devices[0]->titles[1]->id // retrieving the second id

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

Is it possible for me to send transactions asynchronously using polkadot-js?

After thoroughly going through the official documentation, I stumbled upon a page discussing how to transfer using polkadot-js const transfer = api.tx.balances.transfer(BOB, 12345); const hash = await transfer.signAndSend(alice); I am curious if it' ...

Storing multiple entries in a single MySQL cell along with JSON data

I have numerous folders named after different series on my website. Each series folder contains its own chapters, with each chapter containing images. As my website is a manga (comic) site, I want to store the paths of these folders and images in MySQL and ...

Determine if JSON data is empty using PHP and AJAX

My current code sends a partner-code to a PHP script and retrieves JSON data. $('a.pComment').on('click', function() { var partnercode = $(this).attr('data-code'); if($.trim(partnercode) != '') { $.post(&a ...

The issue in AngularJS 1.4 where the select element value is not binding due to a type mismatch

My ng-model has a value assigned to it, but it is not binding with the selected element. <select data-ng-model="myval"> <option value="? number:2 ?"></option> <option value="2" class="ng-binding">Value 1</option> <op ...

Creating a variety of Flexslider slideshows on the fly

Check out this snippet of code: <?php foreach ($objVideos as $objVideo) : ?> jQuery('#carousel-<?php echo $objVideo->id; ?>').flexslider({ animation: "slide", controlNav: false, animationLoop: false, ...

Serve static files using ExpressJS when a post request is made

My Express server is set up to serve static files for my website and it's working fine: var express = require('express'); var app = express(); var path = require('path'); var p = path.join(__dirname, '../web/public'); app ...

Exploring a POST request using jQuery

Apologies if I struggle with my question or use incorrect terminology, as I am new to this. I have a basic web service that processes a JSON string and returns a JSON result. Here is the code: $jsonStr = ''; if(isset($_POST['request'] ...

Ways to transmit data from PHP to JavaScript

I have a file called "hotlaps.php" where I've created a JavaScript script: echo "<body onload=\"myFunction(".$array_1.",".$array_2.",".$array_3.");\">"; In my "hotlaps.js" file, I have the following function: function myFunction(arr ...

The createElement function in Vue.js does not always return a fully formed component

There is a scenario where I need to create a functional component for a specific purpose. The task at hand involves taking all the children elements and adding some additional props to them, but only to those that are custom components of type ChildCompone ...

Vue.js Components Facing Build Issues

I am currently experiencing a puzzling phenomenon. While working on my application components using Laravel Jetstream and Inertia Stack with VueJS, I encountered an issue where a newly created component in the same folder as others was not building. Neithe ...

Leveraging the Power of JavaScript within Angular 12

Currently, I am in the process of learning how to utilize Angular 12 and am attempting to create a sidenav. While I am aware that I can use angular material for this task, I would prefer not to incorporate the associated CSS. My goal is to integrate this ...

The useEffect alert is triggered before the component is re-rendered

Can someone help me understand why the HP in the code below is displayed as "1" when the alert is thrown, and only set to "0" after I confirm the alert? Shouldn't the component be rerendered before the alert is thrown so that it has already been displ ...

Bizarre Object notation with JavaScript

While browsing through a Library called WebApp.net, I stumbled upon this interesting piece of code: var $h = { get HEAD() { return 0 }, get BACK() { return 1 }, get HOME() { return 2 }, get LEFT() { return 3 }, get RIGHT() { return 4 } ...

Troubleshooting Highcharts container selection problem on Nexus 7 running version 4.2.1

Having a problem with Highcharts on my Nexus 7. When I touch the chart, the entire thing gets selected with a blue overlay, but this doesn't happen on other devices like the Nexus 4. Even when I try accessing demos from Highcharts website, the issue ...

JavaScript counter that keeps updating without pause

How can I create a live and continuous number counter on my website? After seeing the question above, I am interested in implementing a similar feature but with a slight twist. I am looking to have a counter that increments by 15.8 cents per second start ...

Enhancing Application Performance Through Next.js Development

I'm currently working on an application using next.js and I am looking to implement code splitting in order to reduce the bundle size and load pages on demand. Unfortunately, I have not been able to find a way to do this without specifying routes. Fo ...

Loading a webpack bundled ngModule dynamically to handle a route efficiently

In an effort to make working on our large frontend projects more manageable, we are looking to split them into multiple independently deployed projects. I am attempting to integrate a bundled ngModule to handle a route from within another app. It is crucia ...

What is the best way to verify the presence of an Object within a JObject and append a new Object if it does not already exist, or merge it with the existing Object

I'm currently working on a solution to verify the presence of Object "key6" within the JSON data displayed below: { "key1": "www.microsoft0nline.nl/test.php", "key2": "2019-06-05 11:21:09", "key3": "otherValue1", "key4": "433", "key5": ...

Having trouble getting JSON data to display in CanvasJS

I am trying to retrieve JSON data using Ajax with this code. It works fine when fetching data from the original source, but it's not working with my own JSON data. What could I be missing? Any help would be greatly appreciated. Thank you. $(document) ...

jQuery not loading properly on the HTML page

I recently created an example.js file that utilizes jQuery. To include jQuery, I used the command npm install jquery and then added the script to my html file like this: <head> <script src="https://code.jquery.com/jquery-3.6.4.min.js"> ...