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

ThiyaneswaranThiyaneswaran Posts: 5Questions: 0Answers: 0
edited March 2019 in Free community support
{
  "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.

Replies

  • ThiyaneswaranThiyaneswaran Posts: 5Questions: 0Answers: 0
    edited March 2019

    This is my code it doesn't show result

    $(document).ready(function () {
        var $table = $('#example').DataTable({
            "'ajax'       : {
        "type"   : "POST",
        "url"    : "http://10.163.14.172:8080/restt/user/search",
         
        "dataSrc": function (data.hits.hits) {
          var return_data = new Array();
          for(var i=0;i< data.hits.hits.length; i++){
            return_data.push({
              'Name': data.hits.hits[i].sourceAsMap.name,
              'Age'  : data.hits.hits[i].sourceAsMap.age,
              'Date' : data.hits.hits[i].sourceAsMap.date
            })
          }
          return return_data;
        }
      },
            "dataType": "json",    
            "columns": [{
                    "data": "Name"
              
                },
                {
                    "data": "Age"
                },
                {
                    "data": "Date"
                }
            ]
            
        });
        
    });
    

    **EDIT:* Updated using Markdown Code formatting.

  • kthorngrenkthorngren Posts: 20,277Questions: 26Answers: 4,766

    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:

          for(var i=0;i< data[0].hits.hits.length; i++){
            return_data.push({
              'Name': data[0].hits.hits[i].sourceAsMap.name,
              'Age'  : data[0].hits.hits[i].sourceAsMap.age,
              'Date' : data[0].hits.hits[i].sourceAsMap.date
            })
          }
    

    However this seems inefficient. I would look at using something like the Nested data objects example. Maybe try setting the ajax.dataSrc to data[0].hits.hits, for example: dataSrc: 'data[0].hits.hits',. Then implement the nested data structure for your columns, like this:

            "columns": [{
                    "data": "sourceAsMap.name"
               
                },
                {
                    "data": "sourceAsMap.age"
                },
                {
                    "data": "sourceAsMap.date"
                }
            ]
    

    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 under url.

    Kevin

  • ThiyaneswaranThiyaneswaran Posts: 5Questions: 0Answers: 0

    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"
    }
    ]

    });

This discussion has been closed.