How to save DAT Table to JSON file?

Hello! I have a “DAT Table”, and I’m using the “DAT File Out” Operator to save this table on my hd (and I use “DAT File In” to read it).
Everything is fine, but I’d like to be able to modify this saved file externally from TD (for example with notepad for Windows, or with VSCode), so I should have a more human readeable file than the standard one.
I’ve tried to change the “Content Languange” option, and the “Edit/View Extension” option of the “DAT File Out” Operator to JSON, but nothing change. The file is always the same.
I’ve tried with CSV format too, but again, nothing change on the saved file.
So, how can I save a file from a table, with the “DAT File Out” Operator, in JSON format (or CSV)?
Many thanks for your help!

Hi @Issimissimo,

Table DATs will always be saved out as tab separated files no matter the extension set in the File Out DAT.
If you want to create a json file from a table, you would have to do so manually where you define how the table is structured into a dictionary. For example will the first column be key values and each following column sub keys?

Let’s consider a table like this:

setting1 setting2
item1 1 2
item2 3 4
item3 5 6

This could be converted into a dictionary and finally a json looking like this:

{
    'item1': {
        'setting1': 1,
        'setting2': 2
    },
    'item2': {
        'setting1': 3,
        'setting2': 4
    },
    'item3': {
        'setting1': 5,
        'setting2': 6
    },
}

To do this, we could use a Script DAT and write:

import json
def onCook(scriptOp):
	scriptOp.clear()
	# create a new dictionary
	newDict = {}
	# get all rows and cols of the input table
	rows = scriptOp.inputs[0].rows()
	cols = scriptOp.inputs[0].cols()
	# loop through all rows, starting with the second row
	for r in rows[1:]:
		# create a new row dict
		newItem = {}
		# loop through all cols, starting with the second column
		for c in cols[1:]:
			# assign the cell value to the dictionary
			colName = c[0].val
			cellValue = r[c[0].col].val
			newItem[colName] = cellValue
		# add the row dictionary to the table dict
		rowName = r[0].val
		newDict[rowName] = newItem
	# write out the dictionary to the Script DAT
	scriptOp.text = json.dumps(newDict, indent=4)
	return

Hope this helps
cheers
Markus

EDIT:
we can optimize this a bit by just accessing the rows - the first row already contains the column header so that is all we need…:

import json
def onCook(scriptOp):
	scriptOp.clear()
	# create a new dictionary
	newDict = {}
	# get all rows and cols of the input table
	rows = scriptOp.inputs[0].rows()
	# get the column headers from the first row
	colHeader = [i.val for i in rows[0][1:]]
	# loop through all rows, starting with the second row
	for r in rows[1:]:
		# create a new row dict
		newItem = {}
		# loop through colHeader list
		for count, c in enumerate(colHeader):
			# assign the cell value to the dictionary
			colName = c
			cellValue = r[count+1].val
			newItem[colName] = cellValue
		# add the row dictionary to the table dict
		rowName = r[0].val
		newDict[rowName] = newItem
	# write out the dictionary to the Script DAT
	scriptOp.text = json.dumps(newDict, indent=4)
	return
3 Likes