How do I turn a list of dictionaries into a table in KDB?

cjm2671

I have decoded JSON to produce a list of the form:

T       v       vw       o      c       h       l        t            n
---------------------------------------------------------------------------
"VMAR"  62737   5.0392   5.19   5.07    5.3     4.9555   1.640639e+12 345
"RADA"  376883  9.6936   9.49   9.64    9.87    9.43     1.640639e+12 3216
"MMLG"  23222   27.5049  27.3   27.54   27.55   27.3     1.640639e+12 89
"ITM"   92400   51.3968  51.4   51.4    51.42   51.38    1.640639e+12 307
"IBDS"  111584  26.5429  26.47  26.55   26.57   26.47    1.640639e+12 270
"VGZ"   441111  0.665    0.675  0.67    0.684   0.66     1.640639e+12 533
"MGTX"  110759  22.3499  22.52  22.53   22.62   21.78    1.640639e+12 1672
"AVID"  241058  32.7345  32.16  32.96   33.08   31.87    1.640639e+12 4512
"VBR"   311815  177.2026 176.54 178.52  178.52  175.47   1.640639e+12 4046
"EBIZ"  25115   27.5717  27.52  27.55   27.74   27.48    1.640639e+12 239
"WWJD"  503027  31.2307  31     31.1874 31.26   31       1.640639e+12 248
"OXACW" 6300    0.4      0.4    0.4     0.4     0.4      1.640639e+12 1
"MSOS"  835924  25.4539  25.41  25.29   25.74   25.15    1.640639e+12 5566

Each row is a dict.

I'm trying to turn it into a table. How can I do this?

Code to reproduce, if you have TLS set up on your KDB:

baseUrl: ":https://api.polygon.io/v2/aggs/grouped/locale/us/market/stocks/"
apiKey: "?apiKey=B__L_1gX9IPlqaSW7JDXv_Ef4i_LhObVOwd_yT"
polyDay: {(.j.k .Q.hg baseUrl, x, apiKey)`results}
r: polyDay["2021-12-27"]
jasonfealy

For a nonconforming list of dictionaries, you can force it to collapse to a table like so:

q)show D:(`a`b!1 2;`b`c!20 30);
`a`b!1 2
`b`c!20 30
q)(distinct raze key each D)#/:D
a b  c
-------
1 2
  20 30

However, as your data contains various datatypes, that solution will result in some columns being of mixed type which is undesirable e.g. below, columns vw & n are contain mixed types

q)meta (distinct raze key each r)#/:r
c | t f a
--| -----
T | C
v | f
vw|
o | f
c | f
h | f
l | f
t | f
n |

The below should work. Credit to Attila Vrabecz, solution originally given here - How to merge a list of tables with different columns

q)\ts t:{raze((uj/)0#'x)uj/:x}enlist each r
70 9854464

or if you know the schema in advance you can simplify to

q)t:0#t // predefined schema
q)\ts t:raze t uj/:enlist each r
34 7768736
q)
q)meta t
c | t f a
--| -----
T | C
v | f
vw| f
o | f
c | f
h | f
l | f
t | f
n | f

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I iterate through nested dictionaries in a list of dictionaries?

kdb/q -- how do I select by customized time list?

How do I convert this list of dictionaries to a table or csv file?

how to turn nested dictionaries into a matrix list?

How do you turn a string that looks like a List of Dictionaries into a List of Dictionaries?

How do I put multiple dictionaries in a list

How do I shuffle a list of dictionaries?

How do I create a list of dictionaries?

How do I turn a list in a .txt file into a list in a python file?

KDB: how to search a table with a list

How do I turn datefinder output into a list?

Turn List of Dictionaries and Into a Set of Dictionaries

How do I add values (list) of a list of dictionaries if their key is same?

How do I convert a list of strings to a list of dictionaries?

How do I column index a keyed table in KDB?

How do I get a list of files in a directory in KDB?

In KDB, how do I sum the previous 3 numbers in a list?

How do I turn a dictionary into a table in KDB?

How do I convert a list into a matrix in KDB?

How do I convert a dictionary of dictionaries into a table?

How do I filter a list in KDB?

How do I create a list of 1's in KDB?

How do I replace items in a list in KDB?

How do I create nested dictionaries with upsert in KDB?

How do I generate a list of times by minute in KDB?

How do I sort a list of dictionaries this way?

How do I turn a tag list into a component?

How do I iterate through a list of dictionaries?

How do I use a for loop with an if statement in a kdb/Q table?

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    pump.io port in URL

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  14. 14

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  15. 15

    How to use merge windows unallocated space into Ubuntu using GParted?

  16. 16

    flutter: dropdown item programmatically unselect problem

  17. 17

    Pandas - check if dataframe has negative value in any column

  18. 18

    Nuget add packages gives access denied errors

  19. 19

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  20. 20

    Generate random UUIDv4 with Elm

  21. 21

    Client secret not provided in request error with Keycloak

HotTag

Archive