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() )
print( is_json(somefunc()) )

convert json arrays into html tables…

import domonic.JSON as JSON

# i.e. containting flat json array of dicts... [{"id":"01","name": "some item"},{"id":"02","name": "some other item"}]

json_data = JSON.parse_file('somefile.json')
mytable = JSON.tablify(json_data)
print(mytable)

convert json arrays into csv files…

import domonic.JSON as JSON

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

convert csv files to json…

import domonic.JSON as JSON

json_data =JSON.csv2json("data.csv")
print(json_data)

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

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.csv2json(csv_filepath: str | Path, json_filepath: str | Path | None = None) str[source]

convert a CSV to JSON.

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.flatten(b: Mapping[str, Any], delim: str = '__') dict[str, Any][source]

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

domonic.JSON.parse(json_string: str | bytes | bytearray) Any[source]

[take a json string and return a python object]

Parameters:

json_string (str) – [a json string]

Returns:

[a python object]

Return type:

[type]

domonic.JSON.parse_file(filepath: str | Path) Any[source]

[loads a json file and returns a python object]

Parameters:

filepath (str) – [path to json file]

Returns:

[a python object]

Return type:

[type]

domonic.JSON.stringify(data: Any, filepath: str | Path | None = None, **kwargs) str[source]

[stringify a python object]

Parameters:
  • data ([type]) – [the python object]

  • filepath (str, optional) – [an optional filepath to save the stringified object] [default: None]

Returns:

[the stringified object]

Return type:

[type]

domonic.JSON.table2json(node) list[dict[str, str]][source]

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

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