0

I am trying to extract specific field from json by filtering data based on it's value instead of Index.

For example my json looks like below

    "AllData": [
        {
            "ID": "1",
            "Value": "Value1"
        },
        {
            "ID": "2",
            "Value": "Value2"
        },
        {
            "ID": "3",
            "Value": "Value3"
        },
        {
            "ID": "4",
            "Value": "Value4"
        },
        {
            "ID": "5",
            "Value": "Value5"
        }
    ]
}

I need to project section (id and value) where value = valueX. But valueX may not always at index X it can be at any other index also. So while projecting I can not use Index. I need to project based on value. I can use contains operator in my where clause which helps to filter the arrays (list of AllData array) as shown below

MyDataSet
| where parse_json(MyJson) contains("Value5")
| project MyJson[5].ID, MyJson[5].Value // this may give wrong result because Value5 can be at some other index

Any Suggestions will be helpful.

1 Answer 1

2

you can use mv-apply: https://learn.microsoft.com/en-us/azure/data-explorer/kusto/query/mv-applyoperator

let my_value = "Value3";
print d = dynamic({"AllData": [
        {
            "ID": "1",
            "Value": "Value1"
        },
        {
            "ID": "2",
            "Value": "Value2"
        },
        {
            "ID": "3",
            "Value": "Value3"
        },
        {
            "ID": "4",
            "Value": "Value4"
        },
        {
            "ID": "5",
            "Value": "Value5"
        }
    ]
})
| mv-apply d = d.AllData on (
    project ID = d.ID, Value = d.Value
    | where Value == my_value
)
ID Value
3 Value3
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks it worked. The only difference is I had my Json in another variable so I could not use dynamic because that was not accepting a predefined variable. So I used pack() function - pack("d",MyJsonVariable),

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.