fix(json-builder): remove base_json, make keys manual text inputs

Changes:
- Removed unnecessary base_json parameter
- Keys are now editable text widgets (not forceInput)
- Values remain as input slots for upstream connections
- Frontend JS updated to add text widgets for keys
This commit is contained in:
科林_Cyril Pilgrim
2026-02-09 23:02:50 +08:00
parent 87d9d055e3
commit 6f349c948d
2 changed files with 45 additions and 41 deletions
+7 -15
View File
@@ -15,12 +15,11 @@ class JSONBuilder:
return {
"required": {
"inputcount": ("INT", {"default": 2, "min": 1, "max": 100, "step": 1}),
"key_1": ("STRING", {"default": ""}),
"key_2": ("STRING", {"default": ""}),
},
"optional": {
"base_json": ("STRING", {"forceInput": True}),
"key_1": ("STRING", {"default": ""}),
"value_1": ("STRING", {"default": "", "forceInput": True}),
"key_2": ("STRING", {"default": ""}),
"value_2": ("STRING", {"default": "", "forceInput": True}),
}
}
@@ -31,21 +30,14 @@ class JSONBuilder:
CATEGORY = "🚦ComfyUI_LLMs_Toolkit/JSON"
DESCRIPTION = """
Build JSON from key-value pairs.
- Adjust **inputcount** and click Update to add more inputs.
- Connect **base_json** to chain multiple builders.
- Keys are manual text inputs
- Values are connected from upstream nodes
- Adjust **inputcount** and click Update inputs to add more pairs
"""
def build(self, inputcount: int, base_json: str = None, **kwargs) -> Tuple[str]:
def build(self, inputcount: int, **kwargs) -> Tuple[str]:
"""Build JSON from key-value pairs."""
# Start with base JSON if provided
if base_json:
try:
result = json.loads(base_json)
except json.JSONDecodeError:
result = {}
else:
result = {}
result = {}
# Add key-value pairs
for i in range(1, inputcount + 1):
+38 -26
View File
@@ -6,45 +6,57 @@ app.registerExtension({
if (nodeData.name === "JSONBuilder") {
nodeType.prototype.onNodeCreated = function () {
this.addWidget("button", "Update inputs", null, () => {
if (!this.inputs) {
this.inputs = [];
}
const inputcountWidget = this.widgets.find(w => w.name === "inputcount");
if (!inputcountWidget) return;
const target_count = inputcountWidget.value;
// Count current key/value input pairs
const current_keys = this.inputs.filter(input =>
input.name && input.name.startsWith("key_")
// Count current key widgets (they are in widgets, not inputs)
const current_keys = this.widgets.filter(w =>
w.name && w.name.startsWith("key_")
).length;
// Count current value inputs
const current_values = this.inputs ? this.inputs.filter(input =>
input.name && input.name.startsWith("value_")
).length : 0;
if (target_count === current_keys) return; // already correct
if (target_count < current_keys) {
// Remove excess inputs (remove from end, pairs at a time)
const pairs_to_remove = current_keys - target_count;
for (let i = 0; i < pairs_to_remove; i++) {
// Find and remove value_N first, then key_N
const idx_to_remove = current_keys - i;
const value_input_idx = this.inputs.findIndex(
input => input.name === `value_${idx_to_remove}`
// Remove excess widgets and inputs
const to_remove = current_keys - target_count;
for (let i = 0; i < to_remove; i++) {
const idx = current_keys - i;
// Remove value input
if (this.inputs) {
const value_idx = this.inputs.findIndex(
input => input.name === `value_${idx}`
);
if (value_idx !== -1) this.removeInput(value_idx);
}
// Remove key widget
const key_widget_idx = this.widgets.findIndex(
w => w.name === `key_${idx}`
);
if (value_input_idx !== -1) this.removeInput(value_input_idx);
const key_input_idx = this.inputs.findIndex(
input => input.name === `key_${idx_to_remove}`
);
if (key_input_idx !== -1) this.removeInput(key_input_idx);
if (key_widget_idx !== -1) {
this.widgets.splice(key_widget_idx, 1);
}
}
} else {
// Add new inputs
// Add new widgets and inputs
for (let i = current_keys + 1; i <= target_count; i++) {
this.addInput(`key_${i}`, "STRING");
// Add key as editable text widget
this.addWidget("text", `key_${i}`, "", () => { }, {});
// Add value as input slot
this.addInput(`value_${i}`, "STRING");
}
}
// Resize node to fit new widgets
this.setSize(this.computeSize());
});
};
}