Skip to main content

json_script

Output value JSON-encoded, wrapped in a < script type="application/json" > tag (with an optional id).

def json_script(
value: any,
element_id: string = None
) - > string

Output value JSON-encoded, wrapped in a < script type="application/json" > tag (with an optional id).

Parameters

NameTypeDescription
valueanyThe Python object to be serialized into JSON format.
element_idstring = NoneAn optional HTML ID attribute to assign to the generated script tag for easy selection via JavaScript.

Returns

TypeDescription
stringA string containing the JSON-encoded data wrapped in a script tag, marked as safe for HTML output.