Search code examples
kql

KQL - Why and when do I need the print command with bag_unpack() or dynamic?


I know the print command outputs a scalar... but why do I need the print command here before bag_unpack, what's it doing?

let JsonDynamic = todynamic('{"hello": 1337, "world": ["wibble","wobble","wubble"]}');
print x = JsonDynamic
| evaluate bag_unpack(x)
| mv-expand world

or, in this stackoverflow answer, why use print with dynamic??? How to transform a JSON array of objects to a Kusto table?

I'm just trying to understand... thank you!!

Sorry this is so vague...I'm trying to understand why I need the print command for this to work?

This doesn't work...

let JsonDynamic = todynamic('{"hello": 1337, "world": ["wibble","wobble","wubble"]}');
| evaluate bag_unpack(JsonDynamic)
| mv-expand world

but JsonDynamic is a dictionary

let JsonDynamic = todynamic('{"hello": 1337, "world": ["wibble","wobble","wubble"]}');
print gettype(JsonDynamic)

This does work but x is still a dictionary?

let JsonDynamic = todynamic('{"hello": 1337, "world": ["wibble","wobble","wubble"]}');
print x = JsonDynamic
| project gettype(x)

They both seem to be dictionaries, but why the does the print command make it work?


Solution

  • I know the print command outputs a scalar

    It doesn't. it outputs a table with single row, with one or more columns (depending on how many scalar expressions you provide it with)

    why do I need the print command here before bag_unpack, what's it doing?

    in your query, the print operator takes the scalar expression you pass into it - JsonDynamic - and emplaces it in a table with a single row and a single column - x. the bag_unpack plugin operates on rows in a specific column in a table - thus, you can't simply use it on a scalar value.

    I'm just trying to understand

    It's not clear what the problem you're trying to solve is. The query you included is valid and works.