Parsing nested array

Parsing nested array

d1nd141d1nd141 Posts: 1Questions: 1Answers: 0
edited December 2021 in DataTables 1.8

Hello,
i have the following json, i'm reading with ajax:

{
    "data": [
        {
            "seen": false,
            "id": "ce3ee69f-4929-4d56-9029-df100f7f8975-xxxxx",
            "message": "[xxx] OSSWIN_PagingFilePrc_M1",
            "status": "open",
            "acknowledged": false,
            "dd": false,
            "tags": [
                "env:PROD",
                "hostname:xxx.xxx.xxx",
                "servicename:SE00098"
            ],
            "count": 2
        },
        {
            "seen": false,
            "id": "ce3ee69f-4929-4d56-9029-xxxxx-1638954580271",
            "message": "[xxx] OSSWIN_PagingFilePrc_M2",
            "status": "open",
            "acknowledged": false,
            "dd": false,
            "tags": [
                "hostname:xxx.xxx.xxx",
                "supportgroup:SUPP.WINDOWS"
            ],
            "count": 2
        }
    ]
}

script:

var table = $('#myTable2').DataTable( {
        "processing": true,
        "serverSide": true,
                "ajax": "./parse_all_local.php",
            "columns": [
                { "data": "id" },
                { "data": "status" },
                { "data": "source" },
                { "data": "message" },
                { "data": "acknowledged" },
                { "data": "createdAt" },
                { "data": "lastOccurredAt" },
                { "data": "priority" },
                { "data": "tags.1" }
            ]
etc

using "tags.1", i always receive the first element.
But i need to put (if existing) the hostname in a column, so the supportgroup etc in another column.
For some events, they don't exist, so the column should be empty.
Also the position in the array is not always the same.
As the tags are not key=value, i probably have to split the string, loop through every item, then fill the column?

Is this possible somehow?
Thanks

Edited by Colin - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    One issue you have is that tags is an array, it would be best to turn that into an object as it would be easier to parse.

    You would then use columns.defaultContent to set a default empty value for the column, then use columns.render as the third parameter is the full row data, and you can just return the value that you want.

    Colin

Sign In or Register to comment.