Hi Roni<o:p></o:p>
<o:p> </o:p>
Looks like this changed in 3.6, in 3.5 they seem to encode differently, but the keyed table doesnt decode properly:
q).j.j flip (col1
col2)!(a
bc
d;til 4)<o:p></o:p>
“[{"col1":"a","col2":0},\n {"col1":"b","col2":1},\n {"col1":"c","col2":2},\n {"col1":"d","col2":3}]”<o:p></o:p>
q).j.j col1 xkey flip (
col1col2)!(
ab
c`d;til 4)<o:p></o:p>
“{"{"col1":"a"}":{"col2":0},"{"col1":"b"}":{"col2":1},"{"col1":"c"}":{"col2":2},"{"col1":"d"}":{"col2":3}}”<o:p></o:p>
q).j.k .j.j col1 xkey flip (
col1col2)!(
ab
c`d;til 4)<o:p></o:p>
'illegal char c at 4<o:p></o:p>
[0] .j.k .j.j col1 xkey flip (
col1col2)!(
ab
c`d;til 4)<o:p></o:p>
^<o:p></o:p>
<o:p> </o:p>
I dont think theres any good way to represent the keyed table structure in JSON, as effectively we have a dictionary where each key & each value is itself a dictionary. In JSON, values can be objects (equivalent to dictionaries) but I dont think names (keys) can be.<o:p></o:p>
<o:p> </o:p>
One alternative that may be a usable approach is something similar to how the bitmex WebSockets API handles this. From their docs https://www.bitmex.com/app/wsAPI#Subscriptions under some traffic there is this example of a JSON message representing a table + meta data:
{<o:p></o:p>
“table”:“orderBookL2_25”,<o:p></o:p>
“keys”:[“symbol”,“id”,“side”],<o:p></o:p>
“types”:{“id”:“long”,“price”:“float”,“side”:“symbol”,“size”:“long”,“symbol”:“symbol”}<o:p></o:p>
“foreignKeys”:{“side”:“side”,“symbol”:“instrument”},<o:p></o:p>
“attributes”:{“id”:“sorted”,“symbol”:“grouped”},<o:p></o:p>
“action”:“partial”,<o:p></o:p>
“data”:[<o:p></o:p>
{“symbol”:“XBTUSD”,“id”:17999992000,“side”:“Sell”,“size”:100,“price”:80},<o:p></o:p>
{“symbol”:“XBTUSD”,“id”:17999993000,“side”:“Sell”,“size”:20,“price”:70},<o:p></o:p>
{“symbol”:“XBTUSD”,“id”:17999994000,“side”:“Sell”,“size”:10,“price”:60},<o:p></o:p>
{“symbol”:“XBTUSD”,“id”:17999995000,“side”:“Buy”,“size”:10,“price”:50},<o:p></o:p>
{“symbol”:“XBTUSD”,“id”:17999996000,“side”:“Buy”,“size”:20,“price”:40},<o:p></o:p>
{“symbol”:“XBTUSD”,“id”:17999997000,“side”:“Buy”,“size”:100,“price”:30}<o:p></o:p>
]<o:p></o:p>
}<o:p></o:p>
<o:p> </o:p>
This has all the associated meta data (key columns, data types, attributes [bitmex runs on kdb+]) in a dictionary where one of the items is also the actual data itself. (This is sent on first subscription, subsequent updates skip the meta data).<o:p></o:p>
<o:p> </o:p>
You could perhaps use a similar structure to transmit your key columns? Essentially similar to your suggestion, but encoding the dictionary as JSON, so I guess itd be more like:
.j.j keys
result!(keys tab;tab)<o:p></o:p>
<o:p> </o:p>
Regards<o:p></o:p>
Jonathon<o:p></o:p>
<o:p> </o:p>
From: personal-kdbplus@googlegroups.com <personal-kdbplus@googlegroups.com> On Behalf Of Roni Hoffman
Sent: 19 March 2019 17:14
To: Kdb+ Personal Developers <personal-kdbplus@googlegroups.com>
Subject: [personal kdb+] How to send json of a keyed qtable<o:p></o:p>
<o:p> </o:p>
Hello, <o:p></o:p>
<o:p> </o:p>
What is best practice for sending a keyed table as json? <o:p></o:p>
<o:p> </o:p>
For example unkeyed and keyed return the same result<o:p></o:p>
<o:p> </o:p>
q).j.j flip (col1
col2)!(a
bc
d;til 4)
“[{“col1”:“a”,“col2”:0},{“col1”:“b”,“col2”:1},{“col1”:“c”,“col2”:2},{“col1”:“d”,“col2”:3}]”
q).j.j col1 xkey flip (
col1col2)!(
ab
c`d;til 4)
“[{“col1”:“a”,“col2”:0},{“col1”:“b”,“col2”:1},{“col1”:“c”,“col2”:2},{“col1”:“d”,“col2”:3}]”
<o:p></o:p>
I have tried by just sending keys separate as a list<o:p></o:p>
<o:p> </o:p>
q)tab:col1 xkey flip (
col1col2)!(
ab
c`d;til 4)<o:p></o:p>
q)(keys
result)!(keys tab;.j.j tab)<o:p></o:p>
(keys
result)!(enlist `col1;“[{“col1”:“a”,“col2”:0},{“col1”:“b”,“col2”:1},{“col1”:“c”,“col2”:2},{“col1”:“d”,“col2”:3}]”)<o:p></o:p>
<o:p> </o:p>
is this recommended? or there a better way?<o:p></o:p>
<o:p> </o:p>
Thanks!<o:p></o:p>
–
Submitted via Google Groups