Mining Data from JSON documents

I am dealing with a webservice that returns JSON data in the following format:

{"d":"{\"RES\":[],\"STAT\":\"FAIL\",\"SID\":\"0\"}"}

So, my question is how can I extract the STAT=FAIL value from this JSON response? The service is written in C#.

This is the script I have implemented:

$.ajax({
    type: "POST",
    url: "http://localhost/EMRDMSService/Service.asmx/User_Login",
    data: "{lg:" + JSON.stringify(GetLogDet) + "}",
    // url: "http://localhost/EMRDMSService/Service.asmx/Permission_List",
    // data: "{userid:" + JSON.stringify(GetLogDet) + "}",

    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (r) {          
        console.log(r.d.STAT);
    }
});

However, when I try to access r.d.STAT, it returns undefined. Can anyone provide assistance on resolving this issue?

Answer №1

Several commenters have pointed out that the JSON object

{"d":"{\"RES\":[],\"STAT\":\"FAIL\",\"SID\":\"0\"}"}
is not formatted correctly. If you are unable to update the webservice, one solution could be to modify your success callback like this:

var d = JSON.parse(r.d);
console.log(d.STAT);

EDIT responding to changes made by the original poster The value of r.d.STAT will be undefined because d is being interpreted as a String rather than an Object. To resolve this issue, you will need to parse it or adjust the webservice to eliminate the quotes surrounding the value of d.

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

Implement a personalized click function for the delete icon in the mini cart

Is there a way to customize the click event for the remove button in the mini cart? function ajax_call(){ $.ajax({ url: ajax_url, type: 'post', data: {action : 'remove_from_cart','cart_item_key' : 10}, ...

A guide on dynamically sending data to a PHP script when an input is changed and displaying the results in a

I am trying to implement a feature where the data inputted into a text field is sent to posttothis.php and then the result is displayed in a content div. However, I am encountering difficulties in making it work. testscript.html <html> <head> ...

Is today within the current week? Utilizing Moment JS for time tracking

There is a problem that I am facing. Can you assist me in determining whether the day falls within the current week? I am currently developing a weather forecast service and need to validate if a given day is within the current week. The only clue I have ...

Passport.js simply redirects to the failure page without invoking the LocalStrategy

I'm facing a persistent issue with Passport.js in my Express.js small application. No matter what I input in the LocalStrategy, I always end up being redirected to the failureRedirect without seemingly passing through the LocalStrategy at all. What am ...

Could someone clarify why EventEmitter leads to issues with global variables?

I recently encountered an error that took me some time to troubleshoot. Initially, I decided to create a subclass of EventEmitter In the file Client.js var bindToProcess = function(func) { if (func && process.domain) { return process.domai ...

When utilizing data-ng-view, AngularJS struggles to locate the corresponding HTML content

I am currently working on building an application using AngularJS, MVC, and API. Below you can find the code for my AngularJS module and controller: //home-index.js var myApp = angular.module('myApp', []); myApp.config([function ($routeProvider) ...

JavaScript is used to dynamically generate HTML content by utilizing jQuery methods

jquery is loaded in my html before my JS code. In my puzzle.js file I have the following: class Puzzle { constructor(ID, Name, Status) { this.ID = "main" + ID; this.Name = Name; this.Status = Status; this.mainDiv = ""; } generateD ...

How can I efficiently generate a table using Vue js and Element UI?

I am utilizing element io for components. However, I am facing an issue with printing using window.print(). It currently prints the entire page, but I only want to print the table section. ...

Is there a way to change the code to interpret ' instead of ’?

Currently working on converting a CSV file to a JSON file. The code seems to be running smoothly until it hits the line: json.dump(DictName, out_file) At which point I'm faced with this error message: UnicodeDecodeError: 'utf8' codec can&ap ...

Guide on transforming JSON (or Plain Old Java Object) into a Room Entity

Using a basic JSON structure as an example: { "widget": { "debug": "on", "window": { "title": "Sample Konfabulator Widget", "name": "main ...

Obtain the value of key2 when the value of key1 is matched?

Here is a JSON object with different colors and quantities. How can you select the quantity based on the color selected by the user from a dropdown menu? { "products": [{ color: "yellow", qty: 22 }, { color: "red", ...

How can JSON enum deserialization be customized in a .NET environment?

Below is a snippet of C# code generated automatically from an xsd using the svcutils.exe application. [DataContract] public enum Foo { [EnumMember(Value = "bar")] Bar = 1, [EnumMember(Value = "baz")] Baz = 2 ...

"Delving into the intricacies of Angular's factory

If I have a factory like the one below: app.factory("categoryFactory", function (api, $http, $q) { var selected = null; var categoryList = []; return { getList: function () { var d = $q.defer(); if(categoryL ...

AngularJS dropdown displays accurate text, yet incorrect value selected

Here is the code for my dropdown selection: <select class="form-control form-controls input-sm" ng-model="vm.retailer.state" ng-options="state.code as state.name for state in vm.states" required> <option value="">-- Select a State --</o ...

Launching the server with a custom path in Nuxt.js: Step-by-step guide

My Nuxt.js application has several nested routes. . ├── index │   ├── _choice │   │   ├── city │   │   │   ├── index.vue │   │   │   ├── _zipCode │   │   │   │   ├── i ...

AngularJS Toggle Directive tutorial: Building a toggle directive in Angular

I'm attempting to achieve a similar effect as demonstrated in this Stack Overflow post, but within the context of AngularJS. The goal is to trigger a 180-degree rotation animation on a button when it's clicked – counterclockwise if active and c ...

Creating a new array by extracting a single property from an array of objects

Currently, I am exploring the most efficient approach to utilize the string-similarity library in NodeJS with the two arrays utilized in my project. The first array consists of objects structured like this: { eventName: "Some event name", ...

Set the packer variable as optional

How can I create a non-required packer variable? For example, consider the code snippet below: { "variables": { "provisioner": null }, When I run this code, I get an error message saying: required variable not set: provisioner What I really nee ...

How to identify duplicate values in a JavaScript array

Is there a way to check for duplicate values in an array and display an alert if any duplicates are found? Here is the function that attempts to do this: function checkDuplicateTenure(){ var f = document.frmPL0002; var supplgrid = document.getElem ...

We encountered a ReferenceError while trying to concatenate strings in JavaScript, indicating that en_EN is not properly defined

In my JavaScript code, I am attempting to combine strings together using the following syntax: var locale = {{ app.request.locale }}_{{ app.request.locale | upper }} + '.json'; The variable {{ app.request.locale }} can be either en, es, fr, or ...