4

After making a POST request using AJAX I get the following JSON response:

    {
"ServiceName": "ABC",
"Response": {
    "Object": [
        {
            "Attributes": {
                "Attribute": [
                    {
                        "AttributeName": "Name",
                        "AttributeValue": "XYZ"
                    },
                    {
                        "AttributeName": "Place",
                        "AttributeValue": "Abc"
                    },
                    {
                        "AttributeName": "Country",
                        "AttributeValue": "Americas"
                    },
                    {
                        "AttributeName": "Code",
                        "AttributeValue": "576"
                    }
                ]
            }
        },
        {
            "Attributes": {
                "Attribute": [
                    {
                        "AttributeName": "Name",
                        "AttributeValue": "XYZHJ"
                    },
                    {
                        "AttributeName": "Place",
                        "AttributeValue": "Abchgh"
                    },
                    {
                        "AttributeName": "Country",
                        "AttributeValue": "India"
                    },
                    {
                        "AttributeName": "Code",
                        "AttributeValue": "536"
                    }
                ]
            }
        }
    ]
}}

I am using datatable to display the data.. but with this nested JSON I am not able to go straight for the data. I am using this https://datatables.net/examples/server_side/post.html https://datatables.net/reference/option/ajax.dataSrc for reference.

2
  • what is your question? where is your code? All you've given us is your JSON response, but no code. How are we supposed to help you? Commented Sep 23, 2015 at 23:17
  • 1
    With server-side processing returned data has different format. What you've posted doesn't match the JSON that is expected to be returned. Commented Sep 24, 2015 at 1:47

1 Answer 1

7

You must iterate over the response and convert it into a format dataTables can comprehend. As I read the sample data you have an Object holding blocks of Attributes holding an Attribute with key => value pairs as AttributeName => AttributeValue. So parse the response in a dataSrc callback :

var table = $("#example").DataTable({
    ajax : {
        url : 'nestedData.json',
        dataSrc : function(json) {
            var temp, item, data = [];
            for (var i=0;i<json.Response.Object.length;i++) {
                temp = json.Response.Object[i].Attributes.Attribute;
                item = {};
                for (var elem in temp) {            
                    item[temp[elem].AttributeName] = temp[elem].AttributeValue
                }
                data.push(item);
            }
            return data
        }
    },
    columns : [
        { data : 'Name', title : 'Name' },
        { data : 'Place', title : 'Place'  },
        { data : 'Country', title : 'Country' },        
        { data : 'Code', title : 'Code' }
    ]    
})

the dataSrc callback return an array of objects on the form :

data = [
  { Code: "576", Country: "Americas", Name: "XYZ", Place: "Abc" },
  { Code: "536", Country: "India", Name: "XYZHJ", Place: "Abchgh" }
]
Sign up to request clarification or add additional context in comments.

Comments

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.