trx
trx

Reputation: 2157

Accessing the value of the JSON data in AngularJS

I am trying to retrieve the value of the JSON data and assign it to the JSON variable in the Angular like this:

$scope.bulkCreateRequest = function (jsonData) {
        var data = {
            "SERVICEREASON": jsonData.ServiceReason,
            "SITE": jsonData.Site,
            "FACILITY": jsonData.Location,
         };
}

When I debug the application though the jsonData has content, it says defined for SERVICEREASON,SITE,FACILITY like below in Local

enter image description here

enter image description here

I am not sure what I am missing here.

Upvotes: 0

Views: 28

Answers (1)

Aks1357
Aks1357

Reputation: 1072

jsonData seems to be an array, so jsonData[0]["ServiceReason"] should work, also first verify if its a string, if it is then you will have to first convert it to array of object like jsonData = JSON.parse(jsonData)

so final code might look like -

$scope.bulkCreateRequest = function (jsonData) {
    jsonData = JSON.parse(jsonData);
    var data = {
        "SERVICEREASON": jsonData[0]["ServiceReason"], // this will also work
        "SITE": jsonData[0].Site,
        "FACILITY": jsonData[0].Location,
    };
};

Upvotes: 2

Related Questions