Search code examples
jsonparsingdictionarykdb

KDB: How to parse a json file?


I created a config file in JSON format, and I want to use KDB to read it in as a dictionary.

In Python, it's so easy:

with open('data.json') as f:
    data = json.load(f)

Is there a similar function in KDB?


Solution

  • To read your JSON file into kdb+, you should use read0. This returns the lines of the file as a list of strings.

    q)read0`:sample.json
    ,"{"
    "\"name\":\"John\","
    "\"age\":30,"
    "\"cars\":[ \"Ford\", \"BMW\", \"Fiat\" ]"
    ,"}"
    

    kdb+ allows for the de-serialisation (and serialisation) of JSON objects to dictionaries using the .j namespace. The inbuilt .j.k expects a single string of characters containing json and converts this into a dictionary. A raze should be used to flatten our list of strings:

    q)raze read0`:sample.json
    "{\"name\":\"John\",\"age\":30,\"cars\":[ \"Ford\", \"BMW\", \"Fiat\" ]}"
    

    Finally, using .j.k on this string yields the dictionary

    q).j.k raze read0`:sample.json
    name| "John"
    age | 30f
    cars| ("Ford";"BMW";"Fiat")
    

    For a particularly large JSON file, it may be more efficient to use read1 rather than raze read0 on your file, e.g.

    q).j.k read1`:sample.json
    name| "John"
    age | 30f
    cars| ("Ford";"BMW";"Fiat")
    

    If you're interested in the reverse operation, you can use .j.j to convert a dictionary into a list of strings and use 0: to save.

    Further information on the .j namespace can be found here. You can also see more examples on the Kx wiki of read0, read1 and 0:.