1

I'm trying to run an Ajax fetch which is apparently successful. What I'm unable to figure out is how to pull the individual field data to separate and use. I can display it all with a data.d.results alert, but what I'd like to do is change my alert to display just one field (i.e. just the email associated with the "extraPOC" field). Any help is much appreciated. I feel like this should be an easy one, but I've looked all over and can't quite get there. The closest I seem to get is to display the alert "object Object".

function findEmail(){
$.ajax({
    url: _xpPageContextInfo.webAbsoluteUrl + "/_api/Web/Lists/GetByTitle('myList')/items?$select=POC/EMail,extraPOC/EMail&$expand=POC,extraPOC&$filter=(ID eq '342')",
    method: "GET",
    headers: { Accept: "application/json;odata=verbose"},
    success: function (data, headers){
        if (data.d.results.length > 0){
            alert (JSON.stringify(data.d.results));
        }
    },
    error: function(error){
        alert("Error: "+ JSON.stringify(error));
    }
});

}

Result from alert("Error: "+ JSON.stringify(error)); :

[{"__metadata":{"id":"Web/Lists(guid'blah)Items(342)","uri":"https://.. blah blah list information... "type":"SP.Data.myListListItem"},"POC":{"results":[{"__metadata":{"id":"f1c17bda-234-233f-23sff3-sdfw3dfs0","type":"SP.Data.UserInfoItem"},"EMail":"test@email.now"}]},"extraPOC":{"results":[{"__metadata":{"id":"324-234a-2343de-13bf38deea","type":"SP.Data.UserInfoItem"},"EMail":"anothertest@email.now"}]}}}]

  • Can you post what you see when you do the alert(JSON.stringify(data.d.results))? – Dylan Cristy Jul 3 '18 at 14:39
  • See edited for the results. Running on a separate network so I kind of summarized but got the important stuff I think. – Chuck.M Jul 3 '18 at 15:34
3

Ok, so I think it's a little easier to see the structure if you open your browser tools and do console.log(data.d.results) instead of alert (no need for JSON.stringify).

Where you see the square brackets indicates an array, so you would have to address that by index, and where you see the curly braces indicates a JSON object, so you can access properties there by using dot notation and the property name.

So data.d.results itself is an array, so to get at the one object that's in the array you need to say data.d.results[0]. Now you have an object with the properties POC, extraPOC, and __metadata:

data.d.results[0]

Since that's an object, you can access the extraPOC property by using dot notation:

data.d.results[0].extraPOC

We can see that the extraPOC property contains another object, with only one property results, which contains an array of only one item. I'll skip a few steps here and go right for that item:

data.d.results[0].extraPOC.results[0]

And finally we can see that that is an object with two properties, Email and __metadata. So to get to the email, we can go for:

data.d.results[0].extraPOC.results[0].EMail

data.d.results[0].extraPOC.results[0].EMail

The reason you have the extra results properties in POC and extraPOC is because they apparently are lookup fields that you are expanding in your query.

  • It's a good thing you're not nearby Dylan, or I'd kiss ya. Worked perfectly!! – Chuck.M Jul 3 '18 at 16:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.