JSON.parse(dataStr) returns undefined - how to get just the string

I am trying to use JSON.parse on a string and it is returning undefined for the object. When I return my string from the web service, It looks like this though:
{"d":"{'AttemptId':'123456'}"}

When I step through it looks like this when I evaluate the expression:
__proto__ = {...}
d = "{'AttemptId':'123456'}"
{'AttemptId':'123456'}

expression
How do I retrieve just the needed part so that I can use the JSON.parse(dataStr)?
{'AttemptId':'123456'}

Here is my code:
             success: function (data) {
                    //   alert('success ');

                    var dataStr = JSON.stringify(data);
                    // alert(dataStr);
                    //By using jquery json parser
                    var obj = $.parseJSON(dataStr);

                    var attemptId = obj['AttemptId'];
                //    alert(obj['AttemptId']);

                    //By using javasript json parser
                    var t = JSON.parse(dataStr);
                    var attemptId2 = t['AttemptId'];
               //    alert(t['AttemptId'])

                },

Open in new window


Thanks!
LVL 2
Starr DuskkASP.NET VB.NET DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

aboo_sCommented:
What did alert(dataStr) give?
and what does alert(data) give?
Starr DuskkASP.NET VB.NET DeveloperAuthor Commented:
alert data:
[object Object]

alert datastr:
{"d":"{'AttemptId':'123456'}"}
(as in my original post)

thanks!
Starr DuskkASP.NET VB.NET DeveloperAuthor Commented:
{"d":"{'AttemptId':'123456'}"}

I thought maybe if I returned 'd' as an object, then retrieved the attemptId from the d object,  but that returns "Function expected"

                    var obj = $.parseJSON(dataStr);
                    var d = obj('d');
                    var attemptId = d['AttemptId'];

Any ideas?
thanks!
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

aboo_sCommented:
Why don't just look for the substring AttemptId':
and then copy everything between ' and '
use indexOf
something like this:
var raw_data=str.substring(str.lastIndexOf("':'")+1,str.lastIndexOf("'}"));

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Starr DuskkASP.NET VB.NET DeveloperAuthor Commented:
yay! I had a couple of other comments that I deleted, having a nightmarish time with this. I finally figured it out.
The stringify is adding escapes, too many in fact, so I did this to finally get it to work:

                    var dataStr = JSON.stringify(data);
                    var raw_data = dataStr.substring(dataStr.lastIndexOf("\":\"") + 3, dataStr.lastIndexOf("\"}"));
                    var replaced = raw_data.split('\\').join('');   // replaces every occurrence of \
                    var t = JSON.parse(replaced);
                    var attemptId = t['AttemptId'];

Open in new window


The data.split removed all the extra escape slashes and made it parseable. Without that line it was returning "invalid character" error.

thanks!
Starr DuskkASP.NET VB.NET DeveloperAuthor Commented:
Aboo-Wan - you're my only hope! thanks!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JSON

From novice to tech pro — start learning today.