KX Community

Find answers, ask questions, and connect with our KX Community around the world.
KX Community Guidelines

Home Forums kdb+ Navigating a nested object using apply.

  • Navigating a nested object using apply.

    Posted by simon_watson_sj on July 12, 2023 at 12:00 am

    Hi All,

    I’m trying to navigate a nested object using apply.

    My original object is called bigNestedThing and application of this:

    .[bigNestedThing;(0;`procData;0;`$”_source”)]

    gets me to a list of dictionaries.  Each of those dictionaries in that list has a key `metadata that I want to access.

    How would I extend that nested key (0;`procData;0;`$”_source”) so it returns a list of values associated with the each `metadata element in the list of dictionaries?

    Ive tried all sorts without success. I would be keen to get my understanding of navigating nested objects nailed. Assuming that previously navigated layers can’t impact the current layer, I imagine there are only a small number of approaches to extend a nested key given the objects encountered as the nest is traversed.

    Simon

    simon_watson_sj replied 9 months, 2 weeks ago 3 Members · 4 Replies
  • 4 Replies
  • rocuinneagain

    Member
    July 12, 2023 at 12:00 am

    Use :: to skip a level

     

     

    q)thing:(`procData`!(((`;`$”_source”)!(`;(`a`metadata!1 2;`b`metadata!3 4));`);`);`) q)thing `procData`!((“_source!(`;(`a`metadata!1 2;`b`metadata!3 4));`);`) ` q).[thing;(0;`procData;0;`$”_source”)] `a`metadata!1 2 `b`metadata!3 4 q).[thing;(0;`procData;0;`$”_source”;::;`metadata)] 2 4

     

    https://code.kx.com/q/ref/apply/#nulls-in-i

     

  • unterrainer_ale

    Member
    July 12, 2023 at 12:00 am

    Hi Simon,

    is there any chance you could provide an example of bigNestedThing? or an extract? In the meantime, try to have a look at cross-section and how it works with apply.

  • simon_watson_sj

    Member
    July 19, 2023 at 12:00 am

    Hi unterrainer_ale – I’m afraid it’s work related and I get enough heat just moving it around internally. I have a todo to get to grips with navigating nested data objects so when I get chance, I’ll write up some cases.

    (Hopefully with solutions!)

    Thanks for having a look and giving it a go. I’ll investigate ‘cross-section’.

    Simon

  • simon_watson_sj

    Member
    July 19, 2023 at 12:00 am

    Thanks  – I’ll give this a go. I was saying to somewhere else I think an issue is that whilst I can correctly initialise my nested dictionaries (starting them with (enlist `)!(enlist ::), any data objects which I’ve de-serialised from JSON come as they are. I had discussed the idea of extending the ‘apply’ form to it could have functionality to either

    • impose a given initialisation per nesting layer rather than taking it from the data object
    • ignore any key errors

    I have a feeling that my second idea might be redundant given your suggestion for the use of :: does pretty much the same thing. Thinking about my first idea – I think this is actually about addressing problems from sets of dictionary key with mixed type. I imagine anything that could handle that would end up being horribly slow. Also I’m not sure on the principle of building native functionality which ultimately only addresses poor data structuring in the first place (unless you stuff it in some sort of separate ‘cleanup’ namespace).

    Thanks again for getting back to me on this I’ll give it a go and report back on results.

    Simon

Log in to reply.