KX Community

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

Home Forums kdb+ Default dictionary value Re: Default dictionary value

  • rocuinneagain

    Member
    February 2, 2022 at 12:00 am

    This blog on JSON parsing may also have some useful tips for you as it covers some areas of nested data with irregular keys

    https://kx.com/blog/kdb-q-insights-parsing-json-files/

     

    An example for your ask is to use a prototype dictionary of default values

    
    
    // Prototype of default values if lookup fails 
    q)p:`a`b`c`d!("X";99;99;99) 
    q)p a| "X" b| 99 c| 99 d| 99 
    // Actual dictionary 
    q)d:`a`b`c!("";2;3) 
    q)d a| "" b| 2 c| 3 
    // Failed lookup uses null of type of first key 
    // a is first key, it is type char, null char is "" 
    q)d`d "" 
    // Prototype can be used by appending your dict to it 
    q)(p,d)`d 99 
    q)(p,d)`b 2 // Handles vectors of keys nicely 
    q)(p,d)`d`b`a 99 2 ""