📝 JSON Editor
Submitted by Lukas Masuch
Summary
An interactive JSON viewer/editor component built with React and CCv2.
Functions
json_editor
Display an interactive JSON editor.
A React-based component that renders JSON data with collapsible nodes and optional editing capabilities. When editing is enabled, users can add, edit, and delete keys/values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict | list | str
|
The JSON data to display. Can be a dict, list, or JSON string. |
required |
name
|
str | None
|
Label for the root node. Use None or False to hide. |
'root'
|
collapsed
|
bool | int
|
Whether to collapse nodes by default. - False: Expand all nodes - True: Collapse all nodes - int: Collapse nodes at depth > int |
False
|
theme
|
str | None
|
Color theme for the viewer. If None (default), auto-detects based on whether the Streamlit theme is light or dark. Options include: "rjv-default", "monokai", "solarized", "ocean", "hopscotch", "summerfruit", "tomorrow", "ashes", "bespin", "brewer", "bright", "chalk", "codeschool", "colors", "eighties", "embers", "flat", "google", "grayscale", "greenscreen", "harmonic", "isotope", "marrakesh", "mocha", "ocean", "paraiso", "pop", "railscasts", "shapeshifter", "twilight" |
None
|
display_data_types
|
bool
|
Show data type labels next to values. |
True
|
display_object_size
|
bool
|
Show object/array item counts. |
True
|
enable_clipboard
|
bool
|
Enable copy-to-clipboard functionality. |
True
|
sort_keys
|
bool
|
Alphabetically sort object keys. |
False
|
editable
|
bool
|
Enable add/edit/delete functionality. |
True
|
key
|
str | None
|
A unique key for this component instance. |
None
|
Returns:
| Type | Description |
|---|---|
JsonEditorState
|
A TypedDict with a "data" key containing the current (possibly edited) JSON. |
Example:
```python
result = json_editor({"name": "John", "age": 30}, key="my_editor")
st.write("Current data:", result["data"])
```
Source code in src/streamlit_extras/json_editor/__init__.py
Import:
- You should add this to the top of your .py file
Examples
example
def example() -> None:
"""Example usage of the JSON editor component."""
st.write("Edit the JSON below:")
sample_data = {
"name": "John Doe",
"age": 30,
"email": "john@example.com",
"active": True,
"tags": ["developer", "python", "streamlit"],
"address": {
"street": "123 Main St",
"city": "San Francisco",
"country": "USA",
},
}
result = json_editor(
sample_data,
name="user",
collapsed=False,
editable=True,
key="demo_editor",
)
st.write("**Current data:**")
st.json(result["data"])