Search code examples
jsonfile-uploadtcl

loading .json file in tcl


I have quite a large (30 kb) .json file, that I want to read inside a script. Till now I just copied the content of the file in my script, but that leads to very ugly code.

This is the code

set allAttrib { <copyOfContent>)
set allAttrib_d [::json::json2dict $allAttrib]

I am sure this should not be that hard, but I do not manage to find an answer somewhere.

Thanks in advance


Solution

  • You need to open and read the file to a string.

    set fp [open "somefile" r]
    set json_string [read $fp]
    close $fp
    set allAttrib_d [::json::json2dict $json_string]
    

    https://wiki.tcl-lang.org/page/How+do+I+read+and+write+files+in+Tcl