Sean
Sean

Reputation: 73

Azure LogAnalytics Parse JSON Array

I am ingesting some custom logs to Azure LogAnalytics. One of the columns contains nested json objects. I would like to return each nested object to a separate column value.

Was trying the mvexpand statement but have not had any luck.

customLog_CL
| extend test = parsejson(target_s)
| mvexpand test

The column data looks like below.

[ { "id": "00phb49dl40lBsasC0h7", "type": "PolicyEntity", "alternateId": "unknown", "displayName": "Default Policy", "detailEntry": "@{policyType=hello}" }, { "id": "0pri9mxp9vSc4lpiU0h7", "type": "PolicyRule", "alternateId": "00phb49dl40lBsasC0h7", "displayName": "All Users Login", "detailEntry": null } ]

Upvotes: 3

Views: 8288

Answers (3)

Sharoon Srivastava
Sharoon Srivastava

Reputation: 21

This should work:

datatable(d:dynamic)  
[  
    dynamic(  
        [  
            { "id": "00phb49dl40lBsasC0h7", "type": "PolicyEntity", "alternateId": "unknown", "displayName": "Default Policy", "detailEntry": "@{policyType=hello}" },   
            { "id": "0pri9mxp9vSc4lpiU0h7", "type": "PolicyRule", "alternateId": "00phb49dl40lBsasC0h7", "displayName": "All Users Login", "detailEntry": "" }  
        ]  
    )  
]  
| mv-expand(d)  
| project key = tostring(d['id']), value = d
| extend p = pack(key, value)
| summarize bag = make_bag(p)
| evaluate bag_unpack(bag)

Output

Upvotes: 2

Danni Juhl
Danni Juhl

Reputation: 31

I'm in the exact same situation, so hopefully we can share the knowledge. I ended up doing something like this, if it's the correct way of doing it, or I have any bugs, I honestly can't tell you right now (still doing my data validation, so I'll update later on), but this should at least get you started.

customLog_CL
| mvexpand parsejson(target_s)
| extend Id=target_s["id"]
| extend type=target_s["type"]
| extend OtherId=target_s["alternateId"]
| project Id, type, OtherId

Upvotes: 3

bharathn-msft
bharathn-msft

Reputation: 962

Please check if this fits your requirement.

    let hosts_object = parsejson('{"hosts": [ { "id": "00phb49dl40lBsasC0h7", "type": "PolicyEntity", "alternateId": "unknown", "displayName": "Default Policy", "detailEntry": "@{policyType=hello}" }, { "id": "0pri9mxp9vSc4lpiU0h7", "type": "PolicyRule", "alternateId": "00phb49dl40lBsasC0h7", "displayName": "All Users Login", "detailEntry": null } ]}');
    print hosts_object 
    | extend json1 = hosts_object.hosts[0] , json2 = hosts_object.hosts[1]

Output for this should be as below

enter image description here

Additional Documentation Reference

Hope this helps.

Upvotes: 0

Related Questions