cancel
Showing results for 
Search instead for 
Did you mean: 

Navigating a nested object using apply.

simon_watson_sj
Contributor

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?

I’ve 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

1 ACCEPTED SOLUTION

rocuinneagain
Valued Contributor
Valued Contributor

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 

 

View solution in original post

4 REPLIES 4

unterrainer_ale
New Contributor III

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. 

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

rocuinneagain
Valued Contributor
Valued Contributor

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 

 

Thanks @rocuinneagain - 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