JSON

domonic includes practical helpers for moving between Python objects, JSON, CSV, and simple HTML tables.

Decorate any function that returns Python objects to return JSON instead:

from domonic.JSON import *

@return_json
def somefunc():
    myObj = {"hi":[1,2,3]}
    return myObj

print( somefunc() )
# {"hi": [1, 2, 3]}
print( is_json(somefunc()) )
# True

Convert JSON arrays into HTML tables:

import domonic.JSON as JSON

# A flat JSON array of dicts, for example:
# [{"id": "01", "name": "some item"}, {"id": "02", "name": "some other item"}]
json_data = [{"id": "01", "name": "some item"}, {"id": "02", "name": "some other item"}]
JSON.stringify(json_data, 'somefile.json')  # write it out first, for this example

loaded = JSON.parse_file('somefile.json')
mytable = JSON.tablify(loaded)
print(mytable)
# <table><tr><th>id</th><th>name</th></tr><tr><td>01</td><td>some item</td></tr><tr><td>02</td><td>some other item</td></tr></table>

Convert JSON arrays into CSV files:

import domonic.JSON as JSON

json_data = JSON.parse_file('somefile.json')
JSON.csvify(json_data, 'data.csv')

print(open('data.csv').read())
# id,name
# 01,some item
# 02,some other item

Convert CSV files to JSON:

import domonic.JSON as JSON

json_data = JSON.csv2json("data.csv")
print(json_data)
# [{"id": "01", "name": "some item"}, {"id": "02", "name": "some other item"}]

The module also includes helpers for validation, flattening, and turning table nodes back into row dictionaries.

API Response Helpers

from domonic.JSON import return_json

@return_json
def api_payload():
    return {"name": "domonic", "features": ["html", "dom", "webapi"]}

print(api_payload())
# {"name": "domonic", "features": ["html", "dom", "webapi"]}

Data to Markup

import domonic.JSON as JSON
from domonic.html import h2, section

rows = [{"name": "Ada", "role": "engineer"}, {"name": "Grace", "role": "compiler"}]
page = section(h2("People"), JSON.tablify(rows))
print(page)
# <section><h2>People</h2><table><tr><th>name</th><th>role</th></tr><tr><td>Ada</td><td>engineer</td></tr><tr><td>Grace</td><td>compiler</td></tr></table></section>

domonic.JSON

JSON and table-conversion helpers used throughout domonic.

This module focuses on practical conversions between Python objects, JSON payloads, CSV data, and simple HTML table structures.

domonic.JSON.parse_file(filepath, **kwargs)[source]

Load a JSON file and return the decoded Python object.

Parameters:

filepath (str | Path) – Path to the JSON file.

Returns:

The decoded JSON value.

Return type:

Any

domonic.JSON.parse(json_string, **kwargs)[source]

Parse a JSON string or bytes object.

Parameters:

json_string (str | bytes | bytearray) – JSON data as text or UTF-8 bytes.

Returns:

The decoded JSON value.

Return type:

Any

domonic.JSON.stringify(data, filepath=None, **kwargs)[source]

Serialize a Python object to JSON.

Parameters:
  • data (Any) – Python value to encode.

  • filepath (str | Path | None) – Optional path to write the JSON payload to.

Returns:

The JSON string.

Return type:

str

domonic.JSON.tablify()[source]

takes a json array and returns a html table

Parameters:

arr (list) – the json array

Returns:

a html table

Return type:

str

domonic.JSON.table2json(node)[source]

Convert a domonic table node back into a list of row dictionaries.

Return type:

list[dict[str, str]]

domonic.JSON.csvify()[source]

takes a json array and dumps a csv file

Parameters:
  • arr (list) – the json array

  • outfile (list) – the output file

Returns:

a csv file

Return type:

str

domonic.JSON.csv2json(csv_filepath, json_filepath=None)[source]

convert a CSV to JSON.

Return type:

str

domonic.JSON.load(filepath, **kwargs)

Load a JSON file and return the decoded Python object.

Parameters:

filepath (str | Path) – Path to the JSON file.

Returns:

The decoded JSON value.

Return type:

Any

domonic.JSON.loads(json_string, **kwargs)

Parse a JSON string or bytes object.

Parameters:

json_string (str | bytes | bytearray) – JSON data as text or UTF-8 bytes.

Returns:

The decoded JSON value.

Return type:

Any

domonic.JSON.dumps(data, filepath=None, **kwargs)

Serialize a Python object to JSON.

Parameters:
  • data (Any) – Python value to encode.

  • filepath (str | Path | None) – Optional path to write the JSON payload to.

Returns:

The JSON string.

Return type:

str

domonic.JSON.json2csv(arr, outfile='data.csv')

csvify

takes a json array and dumps a csv file

Parameters:
  • arr (list) – the json array

  • outfile (list) – the output file

Returns:

a csv file

Return type:

str

domonic.JSON.dump(data, filepath=None, **kwargs)[source]
Return type:

str

domonic.JSON.flatten(b, delim='__')[source]

# i.e. input = map( lambda x: JSON.flatten( x, “__” ), input )

Return type:

dict[str, Any]

domonic.JSON.is_json(value)[source]
Return type:

bool