Retrieving information from JSON files using AngularJS

Here is a JSON object example:

[
{
    "user": "A220",
    "shorttext": "shanghai",
    "reportedBy": "S,A",
    "questions": "

[{\"question\":\"Q1\",\"is_mand\":\"0\",\"type\":\"text\",\"answer\":\"w\",\"ansYesOrNo\":false,\"ansDetails\":\"\"},{\"question\":\"Q2\",\"is_mand\":\"0\",\"type\":\"text\",\"answer\":\"ed\",\"ansYesOrNo\":false,\"ansDetails\":\"\"}]",
        "notifno": "20143995",
        "error": "",
        "createdOn": "2015-09-09 13:08:36",
        "Id": 0,
        "$$hashKey": "object:89"
    }
]

My goal is to access the first question within the questions array. I am encountering issues with trying to do this, as

alert(obj.questions[0].question);
does not work as expected.

To see my problem in action, please check out this jsFiddle link here: LINK to Fiddle

Answer №1

The primary issue lies in the questions array within the object. Instead of being an actual array, it is currently a string. To properly access the JSON data, you need to parse the questions and convert it into an array.

Check out the updated fiddle here: http://jsfiddle.net/uniqueuser123/x94jopq1/2/

Make sure to pay attention to this key piece of code:

var questionObject = JSON.parse($scope.b[0].questions);

Answer №2

To utilize the fromJson function, follow this syntax:

angular.fromJson(a[0].questions)[0].question

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

Removing attribute text from the output in Azure SQL: A step-by-step guide

Is there a way to remove the "attribute" from Azure SQL output for json path? Let's consider the following test data: create table test (country varchar(50), city varchar(50)); insert into test values ('US', 'New York'); insert ...

Is it possible to configure node js to send an array instead of a string?

How can I modify the code to return a 2D array of the results? For example: clothes = [[1, "name", "desc"], [2, "name2", "desc2]] Can the 'res' variable send a list directly or do I need to create a list after returning it? app.get('/post&ap ...

Issues arise when attempting to utilize jQuery to print a string once a button is pressed

I am trying to display a string on the webpage when the user clicks a button. Below is the HTML code: <p> <h1 class="text-center" id="notif"> </h1> </p> Here is the relevant part of the script: $("#btn_My").click(functio ...

I crafted this dropdown menu, but for some reason, the selections aren't registering when clicked. Below is the code I used. Any assistance would be greatly appreciated!

Hi there, I need some help with getting my code to run properly. I've created a dropdown box using HTML and CSS, but it seems like there's an issue with the JavaScript portion as the options are not being selected. I've included a code snipp ...

Unblocking the context menu: How entering JS directly into the address bar compares to using a bookmark

Exploring the concept of blocking the context menu using JavaScript. Here's how you can block such a menu: document.addEventListener('contextmenu', event => event.preventDefault()); I recently came across an article that mentioned this ...

Strategies for streamlining repetitive code within a closure in Angularjs

We are currently utilizing Angularjs 1x and I am in the process of refactoring some repetitive code within an Angularjs filter. However, I am facing challenges in getting it to function correctly. It should be a straightforward task. Our standard approach ...

Error encountered in Internet Explorer 11 - Access is forbidden - XMLHttpRequest

I am encountering a unique issue with IE11 and ajax. Most of the time, everything works as expected when I use the code below for my requests. However, I have noticed that when I try to use it in combination with a copy and paste function, an 'Access ...

Trying to organize JSON data by multiple parameters using jQuery

Regarding the topic discussed in this thread; I have successfully managed to sort an Array of JSON Objects by two fields. Additionally, the discussion mentions: "To include additional columns for sorting, simply add them to the array comparison." // ...

Is there a method we can use to replace fixture fields with data created during the test case to produce a dynamic payload? (Already attempted existing solution)

I am new to using Cypress and I'm wondering if there is a way to generate a dynamic payload by replacing values in a JSON file with values generated programmatically in a Cypress test. This is similar to what we do in Rest Assured by substituting %s i ...

Shifting HTML table in Javascript by toggling checkboxes

When I click the checkbox, the table elements are not displaying inline. I am simply hiding the class "box". Do I need to write a special format? By default, the elements are displayed inline but when I check the checkbox, they shift. The column 'Stat ...

Ways to Export HTML to Document without any borders or colorful text

How can I make a contentEditable area visible when filling text and then disappear when exporting the document? I found a script online that allows you to do this, but the issue is that the contentEditable area is not visible until clicked on. To address t ...

Delineating the execution of a PHP function through a button click using HTML/Javascript

I have a need to trigger a specific PHP function by simply clicking on a button. Currently, I am working with the following code snippet: <table class='standardtable'> <tr> <th>Event</th> <th>Group</th> ...

What is the best way to delete a container when its child element includes a specific string?

I run a website that compiles video clips from various sources, including YouTube. Occasionally, some clips appear as private videos. I am looking to use jQuery to apply the display:none; property to the entire div when the class a.colorbox.cboxElement con ...

Tips for building a sleek vertical carousel website

Looking to set up a sleek vertical carousel for my product list, specifically for a Kiosk Machine that utilizes touchscreen technology. I want the carousel to be smooth and scrollable. I'm a bit unsure about which library or tools to use for this pro ...

Having trouble accessing the latest props within a setInterval in a React functional component

I'm facing an issue where I can't seem to access the updated prop within setInterval inside Component1; instead, it keeps showing me the old value. Here's the code snippet I'm working with: import { useState, useEffect } from "reac ...

I encounter a CORS issue on Heroku, but strangely it only occurs after 20 minutes of deploying. Prior to that time frame, everything functions seamlessly

Today, I encountered a strange issue while hosting my first Node JS backend on Heroku. Everything runs smoothly when I register/login immediately after deploying the backend. However, if I try to register/login after about 15 minutes, I start receiving a C ...

The jquery selector fails to retrieve all elements

On the upcoming web page, I am attempting to use Jquery to select all <li> elements. Specifically, I want to target all the products contained within <ul class=search-result-gridview-items">. You can find the products here: I have made attempt ...

What is the best way to fill in the jquery treeselect widget?

I'm struggling with populating the jquery treeselect widget using a json file or any other method. Since I am new to jquery/javascript, I'm sure I must be missing some basics. Although I have obtained the plugin from https://github.com/travist/j ...

Even though a variable has been assigned a value, it continues to retain its default value

One of the variables in my Model is initially set to null angular.module('qbs.models').service('loginModel', function () { this.name = 'loginModel'; var data = { //....... loggedIn: null, loggedOut: null //. ...

Is it feasible to upgrade from Angular 1.3.9 to the latest version, Angular 1.6.5?

After upgrading from angular 1.3.9 to angular 1.6.5, I encountered the following console error. Uncaught Error: [$injector:unpr] Unknown provider: $$asyncCallbackProvider <- $$asyncCallback <- $animate <- compile No changes were made to my m ...