Merge two dynamic columns and keeping there json data structure in kusto?

131 Views Asked by At
let OriginalTable = datatable(data1:dynamic, data2:dynamic)
[
    dynamic({"name":"Name_1","data":[5,6,7,8]}),
    dynamic({"name":"Name_2","data":[9,10,11,12]})
];

I have this table. How is it possible to merge those two columns under one column keeping the outcome as a json object.

I tried concatenating them using strcat with "," but it changes the structure of the column.

The expected result should be the following:

{
    "name": "Name_1",
    "data": [
        5,
        6,
        7,
        8
    ]
},
{
    "name": "Name_2",
    "data": [
        9,
        10,
        11,
        12
    ]
}
1

There are 1 best solutions below

0
Balaji On

To get output as Json document by merging two dynamic columns, use below code:

let OriginalTable = datatable(data1: dynamic, data2: dynamic)
[
    dynamic({"name":"Name_1","data":[5,6,7,8]}),
    dynamic({"name":"Name_2","data":[9,10,11,12]})
];
OriginalTable
| project data = data1
| union (
    OriginalTable
    | project data = data2
)
| summarize json_data = make_list(pack("name", tostring(data.name), "data", data.data)) 
| project tostring(json_data)

The union operator in the above code is used for combining both data1 and data2 dynamic columns and summarize is used for aggregating the resulting rows and make_list function is applied to aggregate all rows into a single list. Each item in this list is a JSON object generated using pack, which combines the name and data fields from each row.

Output:

[
    {
        "name":"Name_1",
        "data":[5,6,7,8]
    },
    {
        "name":"Name_2",
        "data":[9,10,11,12]
    }
]