Exploring JSON information containing colons in the labels

I am having trouble accessing JSON data in Angular that contains a colon in the label (refer to responsedata). This is the code causing issues:

<li ng-repeat="i in items.autnresponse.responsedata | searchFor:searchString"> <p>{{i.autn:numhits}}</p> </li>

Every time I try to run it, I encounter this error message:

Error: [$parse:syntax] Syntax Error: Token ':' is an unexpected token at column 7 of the expression [i.autn:numhits] starting at [:numhits]
.

Snippet of JSON data:

"autnresponse": {
    "action": {
        "$": "QUERY"
    },
    "response": {
        "$": "SUCCESS"
    },
    "responsedata": {
        "autn:numhits": {
        "$": "92"
    },
    "autn:totalhits": {
        "$": "92"
    },
    "autn:totaldbdocs": {
        "$": "188"
    },
    "autn:totaldbsecs": {
        "$": "188"
    },

Does anyone have a solution for this issue?

Answer №1

After reading the comments, I am confident that I have the answer to my question. Here is what I would say:

Assumption

It seems like your JSON is parsing correctly but your code is having trouble accessing something in the resulting data structure.

Answer

To access the desired element, use square bracket notation with a string:

var x = i['autn:numhits'];

You can also use the same approach if you have a property name stored in a variable, like this:

var propertyName = 'autn:numhits';
var x = i[propertyName];

Addendum

If you are working with Angular template, you can try this:

{{i['autn:numhits']}}

Answer №2

One way to retrieve the value is by using square brackets as if treating it like a dictionary instead of using dot notation. The code {{i.autn:numhits}} can be replaced with {{i['autn:numhits']}}

Just a reminder, if you plan to enclose autn:numhits in double quotation marks, you must escape them properly for HTML rendering.

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

Encountering a 404 error while accessing my meticulously crafted express server

After ensuring that the server is correctly set up and without any errors related to imports or missing libraries, I utilized cors for development purposes. A 404 error persisted even after attempting to comment out the bodyparser. https://i.stack.imgur.c ...

Is it possible to obtain the index of a table row using querySelector?

Is it possible to find the rowIndex of the first occurrence of a table row within the #myTable using JavaScript? http://jsfiddle.net/bobbyrne01/fmj6np6m/1/ html .. <table id="otherTable"></table> <table id="myTable"></table> js ...

React error: Updating state is only allowed on mounted or mounting components

I'm encountering this Error/Warning message in my console: The error message says: setState(...): Can only update a mounted or mounting component. Addressing the Component Mounting Process When it comes to mounting the component, here's the r ...

Changing JSON format into a DataTable using C#

Currently, I am attempting to transform a JSON string into a DataTable within WEBAPI The structure of the JSON string is as follows: [ [ "Test123", "TestHub", "TestVersion", "TestMKT", "TestCAP", ... ...

Neither of the peer models is present in the JSON file

Here is a follow-up question related to a previous one about JSON not being nested in a Rails view. In my application, I have a hierarchy of models that are structured as 1:many relationships as they descend. At the second-from-bottom level, there is a mo ...

Angular 2 Issue: Error Message "Cannot bind to 'ngModel'" arises after FormsModule is added to app.module

I've been struggling with the data binding aspect of this tutorial for over a day now. Here's the link to the tutorial: https://angular.io/docs/ts/latest/tutorial/toh-pt1.html The error I keep encountering is: Unhandled Promise rejection: Tem ...

The validation process did not pass because of a manually inputted value

When a user selects a file, the filename value is automatically inserted into the fileName field. However, the validation fails because the field is still considered empty until at least one more character is added. How can I fix this issue? This is how t ...

Using curly brackets as function parameters

Can someone help me understand how to pass an emailID as a second parameter in curly braces as a function parameter and then access it in AccountMenuSidebar? I apologize for asking such a basic question, I am new to JavaScript and React. class Invoices ex ...

How to detach functions in JavaScript while preserving their context?

Can functions in JavaScript be detached while still retaining access to their context? For instance, let's say we have an instance of ViewportScroller called vc. We can retrieve the current scroll position with the following method: vc.getScrollPosi ...

How to ensure jQuery runs only once, even when the back button is pressed in the

How can I ensure that jQuery only executes once, even when navigating back to the page using the browser's back button? When my page initially loads, the jQuery runs fine. However, if I navigate away from the page and then return using the back button ...

A guide to testing a controller by simulating a service using jasmine

I'm currently in the process of testing an Angular controller by mocking the calls made to a factory using Jasmine. Below is my controller setup: (function () { 'use strict'; function initScoresController(scoresFactory){ var vm = t ...

Can you explain the meaning of "|| [];"?

Can you explain the significance of || [] in this code snippet and provide some insight into why it's included? getPair: function() { return this.props.pair || []; }, ...

Javascript increasing the variable

Whenever I interact with the code below, it initially displays locationsgohere as empty. However, upon a second click, the data appears as expected. For example, if I input London, UK in the textarea with the ID #id, the corresponding output should be var ...

Timing complications with AngularJS directives

I'm having trouble deciphering the sequence of operations here, and it seems like I'm missing the mark. My current project involves integrating a jquery image gallery plugin into a directive. The plugin is designed to load a set of images using ...

Ensuring the accuracy of nested objects through class validator in combination with nestjs

I'm currently facing an issue with validating nested objects using class-validator and NestJS. I attempted to follow this thread, where I utilized the @Type decorator from class-transform but unfortunately, it did not work as expected. Here is my setu ...

issue with implementing the chart.js npm package

Just recently, I added chart.js to my project using npm. My goal is to utilize the package for creating graphs. npm install chart.js --save After the installation, I attempted to import the module with: import chart from 'Chartjs'; However, t ...

Transmitting JSON data to Slack through an HTTP POST submission

I am encountering an issue while attempting to send a message through Slack's chat.postMessage API call. I can successfully encode my test messages in HTTP GET, but I am struggling with achieving the same outcome using JSON in an HTTP POST request. I ...

The issue with AngularJS multiple $http requests failing to fetch accurate data

I'm having issues with my AngularJS controller and service modules. I want to refresh custController.allCustomers after adding a new customer so that the UI displays the new data. However, when I call custController.createCustomer, the new customer do ...

Facing the issue of "Protractor not syncing with the page" while trying to navigate an Angular website

I'm currently attempting to follow the tutorial for Protractor on the official Protractor website, but I've hit a roadblock at step 0. My setup involves using protractor and webdriver-manager version 6.0.0. I am running Linux (Ubuntu 18.06) as m ...

The function webpack.validateSchema does not exist

Out of the blue, Webpack has thrown this error: Error: webpack.validateSchema is not defined Everything was running smoothly on Friday, but today it's not working. No new changes have been made to the master branch since Friday. Tried pruning NPM ...