Accessing nested key value pairs in javascript

Hi guys,

I have been able to implement a sort of password for a javascript package I am using, called jsPsych 5.0. I used a loop function and a survey-text plugin as follows:
 var password = {
      type: 'survey-text',
      questions: [{prompt: "Please enter the password provided to you by email", rows: 3, columns: 40}],
    };

var loop_node_password = {
    timeline: [password],
    loop_function: function(data){
         var Responses = JSON.parse(data[0].responses);
         if (Responses.Q0 == "mypassword"){
              return false;
          }else{
              return true;
              alert("Please enter the correct password")
          }
    }
};

timeline.push(loop_node_password);

Open in new window

 

This worked well.

Now, the problem I have is that var Responses = JSON.parse(data[0].responses); is returning Responses = {"Q0":"mypassword"}. Trying to access it using Responses.Q0 does not seem to work. Any help with this please? I need to access the value of the key Q0, I am not sure how to search for that in internet. I have tried Responses[Q0], Responses["Q0"], nothing works. All I need is to get the value of the "Q0":"mypassword" - i.e. mypassword. Any help please

All the best and thank you,
Dritan
Dritan NikollaResearch AssociateAsked:
Who is Participating?
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.

OMC2000Commented:
try to validate responses:

var key;

for (key in Responses) {
    if (Responses .hasOwnProperty(key)) {
        alert(key + " = " + Responses[key]);
// console.log(key + " = " + Responses[key]);
    }
}

Open in new window

0
Julian HansenCommented:
This should work for the data provided
Responses.Q0

Open in new window

Consider this code
<script>
var json = '{"Q0":"mypassword"}';
var Responses= JSON.parse(json);
console.log(Responses.Q0);
</script>

Open in new window

Output
mypassword

Open in new window


Remember you can also access objects like arrays. For instance if you don't know the property you want and you are dynamically looking for one you can do
Responses["Q0"] is equivalent to Responses.Q0

If your code is not working - post the code so we can see the context.
1

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
Dritan NikollaCommented:
This worked Julian Hansen, thank you very much :). Also, thank you OMC2000 for helping.

Cheers,
Dritan
0
Julian HansenCommented:
@Dritan,

You are welcome. I see this is your first question - Welcome to Experts Exchange - we hope to see more of you on the forums.

It appears your question has been answered - if so could you close the question. If you need assistance with this process please post back here.
0
Dritan NikollaCommented:
Thanks Julian Hansen
0
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
JavaScript

From novice to tech pro — start learning today.