2
var saurabhjson= JSON.stringify(data)

above returns this json

saurabhjson {"recordId":5555,"Key":"5656"} 

if print the first array in console it get undefined value

console.log("saurabhjson[0].recordId",saurabhjson[0].recordId);

i want to do some check like this

 if(saurabhjson[0].recordId == 5555) {
            $('#div_ajaxResponse2').text("another success");
        }  
14
  • 1
    why are you converting the JSON to a string? You can access the values from data itself. Commented Aug 12, 2015 at 12:35
  • 3
    You don't need JSON.stringify(), remove it Commented Aug 12, 2015 at 12:37
  • 2
    @saurabh data.recordId Commented Aug 12, 2015 at 12:41
  • 2
    @saurabh remove this line var saurabhjson= JSON.stringify(data) and simply access the values by data.recordId Commented Aug 12, 2015 at 12:41
  • 1
    @saurabh: There's obviously more going on here than what you're showing us. What is data? Is it a JS object, or a string? Should you be doing JSON.parse() instead of JSON.stringify()? Give us a full example to debug. Commented Aug 12, 2015 at 12:43

2 Answers 2

3

As the method suggests JSON.stringify(data). It converts a js object to a jsonstring now if you want a key out of this string it can't be done before parsing it to json.

So i don't get it why do you need to stringify it.

And another thing is you have a js object not an array of objects. so you need to use this on data itself:

console.log("data.recordId",data.recordId);
Sign up to request clarification or add additional context in comments.

1 Comment

If anyone missed it, OP clarified in a comment on their question that this solution works.
3

You are probably mixing a few things there.

When you do var saurabhjson= JSON.stringify(data), that saurabhjson variable is a string, not an object, so you can't access its elements like you are trying to do.

Try accessing data directly instead, without using JSON.stringify(): console.log("data.recordId",data.recordId);

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.