user1634358 user1634358 - 2 months ago 10x
Ajax Question

How to parse json response for ajax request?

I am newbie to ajax/javascript. I am trying to parse response for below ajax request:

function invokeMediationRestService(rql) {

var resourceinfo;
var request = $
type : "POST",
url : "REST_URL",
async : false,
data : 'SOME_DATA',
contentType : "application/xml",
dataType : "xml",
success: function(response) {


return resourceinfo;


For this request I am expecting response like below:


"items": [
"$param": "Response",
"mode": "OUT",
"$value": [
"resource": [
"firstName": "abc",
"email": "",
"alias": "wert",
"manager": "23ret",
"title": "sdfg",
"location": "rqwe",
"department": "345wre",
"telephone": "143256",
"lastName": "qwer",
"displayName": "qewr, abc",
"containerID": "2345",
"containerName": "2345wert",
"groupID": {
"guid": "234wert65twert53",
"name": "werttyy",
"label": "wwert25345wt",
"$type": "wertsbvhu"
"$type": "wert5363heyetyhg"
"firstName": "qwe",
"email": "",
"alias": "qwer123",
"manager": "asdfrqw3dfsa",
"title": "asdf324",
"location": "qwer32cvx",
"department": "asdfvzxcer",
"telephone": "12345",
"lastName": "rty",
"displayName": "qwe, rty",
"containerID": "12345ewr",
"containerName": "qwer234",
"groupID": {
"guid": "rqwer234wret43t",
"name": "qwer43qwer",
"label": "12345435erwq",
"$type": "12344ewqreqw"
"$type": "1234qwer54r34"
"$type": "qwer2314erq45f"
"$param": "OrganizationModelQuery",
"$value": [
"type": "String",
"mode": "IN"

Please note: There are multiple records under "items[0].$value[0].resource".
I want to extract these records and return them.

Can anyone please help?



It'll probably be a little trial and error - I get a bit confused with JSON at times and forget which sections are arrays and which are just readable.

When I've been dealing with JSON before I use

var json = JSON.parse(JSON.stringify(data));

to get the parsed object. You can then access key/value pairs with something like json.items. If there can be multiple items I think you would want something like json.items[0].$value[0].resource[0].name to read from the right section, but depending on how flexible the layout of your JSON response is you might need to specify the index (using [index]) at other points.