KX Community

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

Home Forums kdb+ Web interface, JSON

  • Web interface, JSON

    Posted by grigora on September 12, 2023 at 12:00 am

    Hi – does anyone have a customization to the .z.ph handler that would allow the caller to receive JSON data, the same way that adding q.csv to the URL would result in a CSV file being returned from the server? TIA

    grigora replied 8 months, 2 weeks ago 2 Members · 6 Replies
  • 6 Replies
  • davidcrossey

    Member
    September 12, 2023 at 12:00 am

    A quick solution could be utilise something like wget to download the json data instead.

    Server

    $ q -p 5000 -q 
    q)show data:([]a:1+til 10;b:10?`4) 
    a b 
    ------- 
    1 milg 
    2 igfb 
    3 kaod 
    4 bafc 
    5 kfho 
    6 jecp 
    7 kfmo 
    8 lkkl 
    9 kfif 
    10 fglg q
    )`:html/test.json 0: enlist .j.j data

    Client

    $ wget http://localhost:5000/test.json 
    --2023-09-12 10:46:40-- 
    http://localhost:5000/test.json Resolving localhost (localhost)... 
    127.0.0.1 Connecting to localhost (localhost)|127.0.0.1|:5000... connected. 
    HTTP request sent, awaiting response... 200 OK 
    Length: 193 [application/json] 
    Saving to: test.json test.json 100%[====================================================>] 193 --.-KB/s in 0s 2023-09-12 10:46:40 (61.1 MB/s)- test.json saved [193/193] 
    $ q -q 
    q)first .j.k each read0 `:test.json 
    a b 
    --------- 
    1 "milg" 
    2 "igfb" 
    3 "kaod" 
    4 "bafc" 
    5 "kfho" 
    6 "jecp" 
    7 "kfmo" 
    8 "lkkl" 
    9 "kfif" 
    10 "fglg"

     

  • grigora

    Member
    September 12, 2023 at 12:00 am

    Thanks, but that doesn’t quite work for my needs. Instead of being able to say http://localhost:5000/.json?callfunc[] and get a JSON, the caller now has to make two requests, one to run the code and save the output on the filesystem, and then another to read the file from html/. And hope there were no requests in between, which can never be guaranteed.

  • davidcrossey

    Member
    September 12, 2023 at 12:00 am

    You can do this in one call without saving the table to the filesystem from the client machine, and without needing to tweak the .z.ph (or .h.val) handlers

     

    Note, if you run the same URL via the brower you’ll see the JSON there, it won’t automatically download like CSV files though.

     

    Hope this helps, in lieu of more community suggestions

  • grigora

    Member
    September 12, 2023 at 12:00 am

    Thanks, that’s good to know. However, it only appears to work with plain tables, not other structures, e.g. dictionary of tables.

    f1:{([]c1:`a`b`c;c2:1 2 3)} / works, but you can do the same with CSV
    
    f1:{ (`t1`t2)! (([]c1:`a`b`c;c2:1 2 3);([]c1:`d`e`f;c2:4 5 6)) } / breaks, even though the structure is serializable through JSON
    wget http://localhost:5000/test.json?f1[] -O test.json
    --2023-09-12 15:27:40-- http://localhost:5000/test.json?f1[]
    Resolving localhost (localhost)... 127.0.0.1
    Connecting to localhost (localhost)|127.0.0.1|:5000... connected.
    HTTP request sent, awaiting response... 400 Bad Request
    2023-09-12 15:27:40 ERROR 400: Bad Request.
  • grigora

    Member
    September 13, 2023 at 12:00 am

    enlist did the trick, thanks. The following works fine, no need for double .j.j / .j.k

    f1:{enlist (`t1`t2)!(([]c1:`a`b`c;c2:1 2 3);([]c1:`d`e`f;c2:4 5 6))}
    wget http://localhost:5000/test.json?f1` -O test.json
    
    q).j.k raze read0`:test.json
    t1| +`c1`c2!((,"a";,"b";,"c");1 2 3f)
    t2| +`c1`c2!((,"d";,"e";,"f");4 5 6f)

     

  • davidcrossey

    Member
    September 13, 2023 at 12:00 am

    Try enlisting

     

     

     

Log in to reply.