How to create data table using this Json to take SourceAsMap name,age and date
How to create data table using this Json to take SourceAsMap name,age and date
Thiyaneswaran
Posts: 5Questions: 0Answers: 0
{
"data": [
{
"scrollId": null,
"totalShards": 5,
"successfulShards": 5,
"skippedShards": 0,
"shardFailures": [],
"clusters": {
"total": 0,
"successful": 0,
"skipped": 0,
"fragment": true
},
"hits": {
"hits": [
{
"score": 1,
"id": "2",
"type": "_doc",
"nestedIdentity": null,
"version": -1,
"fields": {},
"highlightFields": {},
"sortValues": [],
"matchedQueries": [],
"explanation": null,
"shard": null,
"index": "check1",
"clusterAlias": null,
"sourceAsMap": {
"date": "2019-10-03",
"name": "moorthi",
"age": 21
},
"innerHits": null,
"sourceRef": {
"childResources": [],
"fragment": true
},
"sourceAsString": "{\"name\":\"moorthi\",\"age\":21,\"date\":\"2019-10-03\"}",
"fragment": false
},
{
"score": 1,
"id": "1",
"type": "_doc",
"nestedIdentity": null,
"version": -1,
"fields": {},
"highlightFields": {},
"sortValues": [],
"matchedQueries": [],
"explanation": null,
"shard": null,
"index": "check1",
"clusterAlias": null,
"sourceAsMap": {
"date": "2019-10-01",
"name": "thiyanesh",
"age": 21
},
"innerHits": null,
"sourceRef": {
"childResources": [],
"fragment": true
},
"sourceAsString": "{\"name\":\"thiyanesh\",\"age\":21,\"date\":\"2019-10-01\"}",
"fragment": false
}
],
"totalHits": 2,
"maxScore": 1,
"fragment": true
},
"aggregations": null,
"failedShards": 0,
"suggest": null,
"took": {
"millis": 2,
"hours": 0,
"minutes": 0,
"seconds": 0,
"days": 0,
"nanos": 2000000,
"micros": 2000,
"hoursFrac": 5.555555555555555e-7,
"microsFrac": 2000,
"minutesFrac": 0.000033333333333333335,
"secondsFrac": 0.002,
"daysFrac": 2.3148148148148148e-8,
"millisFrac": 2,
"stringRep": "2ms"
},
"timedOut": false,
"numReducePhases": 1,
"terminatedEarly": null,
"profileResults": {},
"fragment": false
}
]
}
EDIT: Updated to use Markdown code formatting.
This discussion has been closed.
Replies
This is my code it doesn't show result
**EDIT:* Updated using Markdown Code formatting.
I would suggest using console.log statements to validate the for loop is processing the data as expected. The
data
object looks to be a single element array but you aren't referencing this in the for loop. You likely are getting errors in your browser's console. The for loop probably needs to look like this:However this seems inefficient. I would look at using something like the Nested data objects example. Maybe try setting the
ajax.dataSrc
todata[0].hits.hits
, for example:dataSrc: 'data[0].hits.hits',
. Then implement the nested data structure for your columns, like this:Let us know if this helps.
Note also that the
"dataType": "json",
is an ajax config option but is outside the ajax object. You should move it inside the ajax option, maybe underurl
.Kevin
The correct solution is:
we made a slight change and we got a data Table
Thank you!!!
var $table = $('#example').DataTable({
'ajax': {
"type": "POST",
"url": "http://localhost/ajax/thi/min.json",
"dataType": 'json',
"dataSrc": function (json) {
console.log(json.data[0].hits.hits);
var response_data1 = [];
for (var i = 0; i < json.data[0].hits.hits.length; i++) {
response_data1.push({
'Name': json.data[0].hits.hits[i].sourceAsMap.name,
'Age': json.data[0].hits.hits[i].sourceAsMap.age,
'Date': json.data[0].hits.hits[i].sourceAsMap.date
})
}
console.log(response_data1);
return response_data1;
}
},
"columns": [{
"data": "Name"
},
{
"data": "Age"
},
{
"data": "Date"
}
]
});