Extracting data from a nested JSON array within an AngularJS template

Here is some JSON data:

{
"tracks": [
    {
        "album": {
            "released": "2013",
            "href": "spotify:album:3qGeRY1wt4rrLIt1YuSwHR",
            "name": "The Marshall Mathers LP2 (Deluxe)",
            "availability": {
                "territories": "AD AR AT AU BE BG BO BR CH CL CO CR CY CZ DE DK DO EC EE ES FI FR GB GR GT HK HN HR HU IE IS IT LI LT LU LV MC MT MY NI NL NO NZ PA PE PH PL PT PY RO SE SG SI SK SV TR TW UY"
            }
        },
        "name": "The Monster",
        "popularity": "0.94",
        "external-ids": [
            {
                "type": "isrc",
                "id": "USUM71314082" <-- The desired value
            }
        ],
        "length": 250.188,
        "href": "spotify:track:5U8hKxSaDXB8cVeLFQjvwx",
        "artists": [
            {
                "href": "spotify:artist:7dGJo4pcD2V6oG8kP0tJRR",
                "name": "Eminem"
            },
            {
                "href": "spotify:artist:5pKCCKE2ajJHZ9KAiaK11H",
                "name": "Rihanna"
            }
        ],
        "track-number": "12"
    },
    ...
]}

I can extract other values like album name and release date, but am struggling with the external-ids id. I have attempted two solutions in my Angular template:

<div class="panel panel-success" ng-repeat="track in something.tracks">
    <p>Track name: {{ track.name }}</p>
    <p>Album release date: {{ track.album.released }}</p>
    <p>Spotify id: {{track.external-ids[0].id}} </p>

    <!-- <span ng-repeat="obj in track.external-ids">  
            <p>Spotify id: {{ obj.id }}</p>
        </span> -->

</div>

Both solutions have failed - the first returns 0, while the second doesn't display the "Spotify id:" line at all.

If anyone knows how to achieve this, please share your insights. Thank you.

Answer №1

When dealing with properties that include a dash, it is important to utilize the bracket notation:

<p>Example id with dashes: {{track['external-ids'][0].id}} </p>

Failure to do so may result in the parser interpreting it as a subtraction operation:

track.external - ids[0].id

See a Demo here: http://plnkr.co/edit/gIyXg8y0Pn1TuK0uI6qD?p=preview

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

Multiplying the values enclosed within two characters within a PHP string

Make sure to check out the EDIT section at the end. I have encountered invalid json data from an external URL that I need to parse and update the values of specific keys. My goal is to multiply the values by a constant number for a particular key (cent). ...

Textbox textchange event triggers Javascript function when Checked=True

$('.LblQTY').on('input', function () { var currentRow = $(this).closest('tr'); var rate = parseFloat(currentRow.find('.LblRate').text()); var quantity = parseFloat($(this).val() == '' ? '0& ...

Form with several file selection points

I am working on an application that features a dynamic form capable of uploading files to different individuals simultaneously. Each person is assigned a unique input file and has the option to add up to 2 additional dynamic inputs. <div id="InputsWra ...

The scrolltop function is dysfunctional on CentOS operating systems

I'm currently working on implementing smooth scrolling functionality when a button is clicked. The feature works perfectly fine with a local Apache server and IE10+, but when the project is deployed on "CentOS", it doesn't work on the same browse ...

Having trouble accessing env variables from React Component in Next.js?

I recently set up a Next.js project and included an .env file to store environment variables used in my server.js file. However, I am facing an issue when trying to access these variables from a component. Can anyone provide guidance on how to resolve this ...

Looking to display a div with both a plus and minus icon? Having trouble getting a div to show with left margin? Need assistance hiding or showing div text

Can someone please review this source code? Here is the demo link: http://jsfiddle.net/bala2024/nvR2S/40/ $('.expand').click(function(){ $(this).stop().animate({ width:'73%', height:'130px' }); $( ...

What is the Vue.js equivalent of Angular's ng-container?

When working with Angular, you may come across a tag called ng-container which is used in the following way: <ng-container *ngIf="false">this won't be shown</ng-container> According to the Angular documentation: The Angular is a grou ...

There seems to be an issue with Material UI Autocomplete not responding to the onChange value

I am currently developing a project using reactjs and incorporating material ui to create components. One specific component I have utilized is the Autocomplete within a form for event creation in my project. I am encountering an issue where I want the sta ...

Attempting to unveil concealed download URLs

Trying to extract download links from a website, but the format is as follows: <form action="" method="post" name="addondownload" id="addondownload" > <input type="hidden" name="addonid" id="addonid" value="2109" /> <input class="re ...

Having trouble retrieving the iframe document using JavaScript

I'm currently facing an issue when trying to fetch an image from a website (not specifically Bing, this problem arises with every site). Upon running the code, it seems to be failing at the 'if' statement, indicating that there might not b ...

The Wikipedia API is unable to be loaded using the XMLHttpRequest

I have encountered this error before on this platform, and although I managed to fix it, I am seeking a more in-depth explanation of the solution. For a project where I am learning, I decided to create a Wikipedia Viewer application. The first step was to ...

Exploring the wonders of looping through arrays with JavaScript and jQuery

I am having trouble getting this function to work properly. My goal is to replace certain characters when a key is pressed. The code works fine using the variable `replace_list` instead of `replace_list["russian"]`, but I actually need different "replace ...

Unlocking the Secrets: Deserializing a Confidential Discriminated Union using Newtonsoft.Json in F#

Back in 2018, Onur Gumus asked a question on Stack Overflow regarding serialization and deserialization of a discriminated union with a private constructor. He utilized [JsonObject(MemberSerialization = MemberSerialization.Fields)] to achieve this function ...

Best Practices for Utilizing Bootstrap 5 Tooltip and Popover Methods in AngularJS Web Applications

Our AngularJS 1.8.2 application requires an upgrade to Bootstrap 5 in order to properly support Tooltips, Popovers, and Modals. Previously, we had been utilizing ui-bootstrap for handling popups. However, migrating from ui-bootstrap to Bootstrap 5 has prov ...

Direct attention to the modal display

I'm having trouble auto-focusing an input field when a modal is displayed. Here's what I've tried, but it doesn't seem to be working: jQuery(document).ready(function($) { $('#myModal').on('show.bs.modal', functi ...

What is the best approach for managing _app.js props when transitioning from a page router to an app router?

Recently, in the latest version of next.js 13.4, the app router has been upgraded to stable. This update prompted me to transition my existing page router to utilize the app router. In _app.jsx file, it is expected to receive Component and pageProps as pr ...

Generate a dynamic request by progressively incrementing variable values within JMeter

Here is the response from a request: **{ "Packages":[ { "CreatedBy":"Administrator", "CreatedDate":"\/Date(1535635263383)\/", "DeviceFamily":6, "LastVersion":{ "BuildVersion":"1.0 ...

The input form in my Node.js project is not adapting to different screen sizes. I am currently utilizing ejs as the template

I have integrated ejs as a template in my Node.js project, but I am encountering an issue with the input form in the following code snippet. The form is unresponsive, preventing me from entering text or clicking on any buttons. What could be causing this ...

Listcell XUL with button options

How can I make buttons inside a listcell function in XUL? I am having trouble getting it to work. Here is the XUL code: <listitem id = "1"> <listcell label = "OK Computer"/> <listcell label = "Radiohead"/> <listcell label ...

Ensure to first close the existing global connection before attempting to establish a new one in your Node.js Post WebApi application

Currently, I am in the process of creating a small post WebAPI using node.js to collect user data such as names and numbers. However, when I have an excessive number of users accessing this web API, I encounter the following error message: "node.js Global ...