1

I have some valid JSON as follows

[
    {
        "userFullName": "Tim, Bill",
        "id": "LOt3",
        "organisation": "FAP",
        "loginSystem": "A",
        "userId": 0
    },
    {
        "userFullName": "Bruce, David",
        "id": "LNA",
        "organisation": "ES",
        "loginSystem": "A",
        "userId": 0
    }
]

I am attempting to access the JSON elements in the success of an AJAX call as follows:

success: function (data) {
    console.log('data ' + data);
    $.each(data, function (key, value) {
        console.log('id' + data[key].id);
        $('#selectStaff').append('<option value="' + data[key].id + '">' + data[key].id + '</option>');
    });
}

But data[key].id is returning undefined and if I just print out data[key], I get the individual characters of the array.

selectStaff is the ID of a SELECT.

What am I missing ?? Any help will be much appreciated.

Thanks

5
  • Try console.log(data) to see what the data variable contains Commented Nov 8, 2013 at 11:21
  • typeof data === string, typeof($.parseJSON(data)) === object tada! Commented Nov 8, 2013 at 11:24
  • @palaѕн I have tried value.id and have got the same result. Commented Nov 8, 2013 at 11:25
  • @AlexanderKuzmin console.log(data) gives me the JSON that I have printed in the question Commented Nov 8, 2013 at 11:26
  • Please amend your question to show the rest of the ajax call. You're getting flooded with responses that you need parse data to create an object, when you don't. Commented Nov 8, 2013 at 11:30

5 Answers 5

2

well either you have to use JSON.parse(data) or add dataType option to your ajax function, so it knows the reponse is in JSON format and nothing else.

....
 dataType:"json",
success: function (data) {
     javascript: console.log('data ' + data);
     $.each(data, function(key, value) {
     javascript: console.log('id' + data[key].id);
     $('#selectStaff').append('<option value="' + data[key].id+ '">' + data[key].id+ '</option>');
       });
}

or

 success: function (data) {
     javascript: console.log('data ' + data);
     data=JSON.parse(data);
     $.each(data, function(key, value) {
       .......
Sign up to request clarification or add additional context in comments.

2 Comments

I have dataType : json set with the same result. Actually I was using datatype with a lowercase t. Changing it to upper case fixed it. I will accept your answer Bipen.
welcome... yes i was about to comment on @rory's post.. about that t in dataType..he had it in samll letters...anyways glad it helped.. happy coding.. :)
1

Your code works in a Fiddle when data is defined as an object.

Given that you state:

if I just print out data[key], I get the individual characters of the array.

it sounds like the result of your $.ajax call is returning a string, not deserialised JSON. You can use the dataType parameter to force the deserialisation:

$.ajax({
    dataType: 'json',
    // rest of your settings...
});

2 Comments

I have datatype" 'json' set in the AJAX call
Your answer was correct as well as incorrect. I had datatype defined just as put it, but it worked after I changed it to dataType.
0

try parsing json with jquery:

success: function (data2) {
    var data=jQuery.parseJSON(data2);
    console.log('data ' + data);
    $.each(data, function (key, value) {
        console.log('id' + data[key].id);
        $('#selectStaff').append('<option value="' + data[key].id + '">' + data[key].id + '</option>');
    });
}

Comments

0

Check type of your data parameter, you want to receive an object. If it's not an object then you need to parse it.

You can either specify the datatype which handles it for you:

$.ajax({
    datatype: 'json',
    // ...
});

Or you can parse it manually:

if (typeof data === "string") {
    data = $.parseJSON(data);
}

Comments

0

You need to parse json.

http://api.jquery.com/jQuery.getJSON/

http://api.jquery.com/jQuery.parseJSON/

Parse JSON in JavaScript?

// Assign handlers immediately after making the request,
// and remember the jqxhr object for this request
var jqxhr = $.getJSON( "example.json", function() {
  console.log( "success" );
})
  .done(function() {
    console.log( "second success" );
  })
  .fail(function() {
    console.log( "error" );
  })
  .always(function() {
    console.log( "complete" );
  });

// Perform other work here ...

// Set another completion function for the request above
jqxhr.complete(function() {
  console.log( "second complete" );
});

(The above is taken from http://api.jquery.com/jQuery.getJSON/)

2 Comments

Not if he specifies the dataType
true. Thats why the first link api.jquery.com/jQuery.getJSON shows how to set datatype to json. I gave three links for @John to see that there are different ways of doing things. Also he will get more idea about whats going on. Good for research.

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.