1

I am unable to access a simple JSON Element from a JSON Structure that looks like:

{
"ACTION": "AA",
"MESSAGE": "Customer: 30xxx Already Approved on 2017/01/01"
}

I get the data in JSON Format but when i do data.ACTION or data.MESSAGE i get Undefined as the output.

By doing case sensitive also, its not working( Image attached ) enter image description here

var url = base + query;
var getJSON = function (url) {
    return new Promise(function (resolve, reject) {
        var xhr = new XMLHttpRequest();
        xhr.open('get', url, true);
        xhr.withCredentials = true;
        xhr.onload = function () {
            var status = xhr.status;
            if (status == 200) {
                resolve(xhr.response);
            } else {
                reject(status);
            }
        };
        xhr.send();
    });
};

getJSON(url).then(function (data) {
    console.log(data); //Getting JSON Data                   
    var output = JSON.stringify(data);
    var obj = JSON.parse(output.replace(/ 0+(?![\. }])/g, ' '));
    console.log(output);
    console.log(obj.message); //Here getting UNDEFINED                    

}, function (status) { //error detection....
    alert('Something went wrong.');
});

Console:
{"ACTION":"AA","MESSAGE":"Customer No. 0000030332 Already Approved On 20170113"}

stringify returns the following
{\"ACTION\":\"AA\",\"MESSAGE\":\"Customer No. 0000030332 Already Approved On 20170113\"}"
9
  • 2
    Why all the stringify/parse thing? JSON.parse(data) should suffice. Commented Jan 17, 2017 at 19:47
  • 2
    javascript is case sensitive, it's obj.MESSAGE Commented Jan 17, 2017 at 19:47
  • Data is already coming in JSON Format. I was just testing it out. data and obj return the same JSON output( mentioned in the top of the post ). If you do JSON.Parse on a Parsed data it gives error. Commented Jan 17, 2017 at 19:48
  • You don't need all the parsing, and the JS is case sensitive. Just simple use data.MESSAGE Commented Jan 17, 2017 at 19:49
  • For future reference, you can quickly check what calls for what in an object by using this simple and quick script: for (var x in obj) console.debug('obj.'+x, "\t=\t", data[x]) Just replace the 2 obj's with the name of your object, in this case data, which would look like: for (var x in data) console.debug('data.'+x, "\t=\t", data[x]) Commented Jan 17, 2017 at 19:53

4 Answers 4

1

EDITED. I first thought the error was due to parsing, given the print. -.-

Solution: When you print the output, obj it's still a string, not an object. So it is OK at that point.

Your "undefined" property message should be replaced by MESSAGE. Instead of console.log(obj.message); just use console.log(obj.MESSAGE);

Also. An example of parsing JSON:

var myJson = '{"ACTION":"AA","MESSAGE":"Customer No. 0000030332 Already Approved On 20170113"}';
console.log(myJson);   // This prints the literal string  
console.log(JSON.parse(myJson)); // this prints an "object"
Sign up to request clarification or add additional context in comments.

2 Comments

But if he's using getJSON, then it's already parsed, if passed correctly by the backend controller. Might be an issue of not properly using json_encode or something of the sort.
Looked like data was not proper JSON from Server
1

obj.message property is not defined and when you try to get the property which is not defined on an object, you get undefined.

Javascript is case sensitive. You should try obj.MESSAGE instead to get the property value. Also, to check if a property exists on an object you can make use of object.hasOwnProperty([propName]) method to check if a property exists on a object or not.

EDIT 1: Try running the following code snippet. JSON data string is parsed before accessing the property.

var jsonString = "{\"ACTION\":\"AA\",\"MESSAGE\":\"Customer No. 0000030332 Already Approved On 20170113\"}";

var obj = JSON.parse(jsonString);
console.log(obj.MESSAGE);

2 Comments

Thanks for the reply. But still not working. I have added picture for reference
Edited the answer, please check.
1

data already is a JSON string, there's no need to JSON.stringify it (which returns a string with a JSON-encoded string literal). Parsing it into output only leads to a string again, which has no properties. You should use

console.log(data);
var obj = JSON.parse(data);
console.log(obj);
obj.MESSAGE = obj.MESSAGE.replace(/ 0+(?![\. }])/g, ' ');

(notice the proper casing of the property name)

Comments

0

You can try:

var jsonObject = data.data;
console.log(jsonObject.ACTION)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.