Hi,
While building relational data in Grist, I want to represent it in a hierarchical format like JSON. I could not find another lightweight tool that performs this simple task.
Grist does not natively export data to JSON, which I think could be a great benefit for the tool. All export options are limited to “2D” CSV family formats.
JSON formatted data can be achieved thanks to formulas, which are powerful due to Python support. However, having the JSON data inside Grist is not the final solution; it needs to be extracted somehow.
Given that Grist operates in a sandbox environment, how can the data be extracted to a filesystem?
Thei code is following
import json
def process_dict(input_dict):
output_dict = {}
for key, value in input_dict.items():
if ISREFLIST(value):
value = [] if not value else process_dict(RECORD(value)[0])
elif ISREF(value):
value = process_dict(RECORD(value))
elif not value:
value = [] if isinstance(value, list) else ''
output_dict[key] = value
return output_dict
f = open(str(rec.input.id) + '.json', 'w', encoding="utf-8")
jsonbody = json.dumps(process_dict(RECORD(rec.input)))
f.write(jsonbody)
f.close()
return jsonbody
no file gets created.
The use case is we want to decide if we can use the Grist tool for building test data and being able to export it into JSON files so the tests could pick them up.
We see a real opportunity in being able to automate the exporting activity in Grist while wotking in it; however, the sandbox environment limits us.
Thanks.