Skip to content

Instantly share code, notes, and snippets.

@idelem
Last active May 30, 2022 06:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save idelem/c5a8e743d0ba7180b5a1834e2e8ec649 to your computer and use it in GitHub Desktop.
Save idelem/c5a8e743d0ba7180b5a1834e2e8ec649 to your computer and use it in GitHub Desktop.
export nested json for treesheets

the motivation is to export game config json data from TreeSheets. the original script is not very useful, so i made my own modifications on top of it.

now a grid like this:

image

will export to this:

{
  "more_list_of_things": [
    {
      "key": [
        "value",
        "value 1.2"
      ]
    },
    {
      "key2": "value2"
    }
  ]
}

known issues:

  • it's really hard to determine if a child is a list or a dictionary
import std
let out = []
def add_text(s):
out.push("\"")
// \ and " need to be escaped in JSON:
out.push(escape_string(s, "\\\"", "\\", ""))
out.push("\"")
def add_indent(indent):
out.push(concat_string(map(indent): " ", ""))
def add_cell(indent):
out.push("{\n")
add_indent(indent + 1)
add_text(ts_get_text())
let child_count = ts_num_children()
if (child_count) > 0:
out.push(": ")
if (child_count) > 1:
out.push("[\n")
for(child_count) i:
ts_goto_child(i)
if child_count > 1:
add_indent(indent + 2)
if ts_num_children():
add_cell(indent + 2)
else:
add_text(ts_get_text())
ts_goto_parent()
if i < child_count - 1:
out.push(",")
out.push("\n")
if (child_count) > 1:
add_indent(indent + 1)
out.push("]")
out.push("\n")
add_indent(indent)
out.push("}")
let fn = ts_get_filename_from_user(true)
if fn.length:
ts_goto_root() // This the default, here just for clarity.
add_cell(0)
let ok = write_file(fn, concat_string(out, ""))
ts_set_status_message("JSON export " + if ok: "succesful" else: "failed!")
else:
ts_set_status_message("export cancelled")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment