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
- 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]
- domonic.JSON.parse_file(filepath: str | Path) Any[source]
[loads a json file and returns a python object]
- domonic.JSON.stringify(data: Any, filepath: str | Path | None = None, **kwargs) str[source]
[stringify a python object]