From f2619d4bf5f1f133b8f83c9c1ceb9618f2a8fab7 Mon Sep 17 00:00:00 2001 From: Dennis Kobert Date: Wed, 28 Jan 2026 13:23:19 +0100 Subject: [PATCH 1/2] Add JSON get node --- node-graph/nodes/gcore/src/logic.rs | 34 +++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/node-graph/nodes/gcore/src/logic.rs b/node-graph/nodes/gcore/src/logic.rs index 0b21b5d956..1b0f7866be 100644 --- a/node-graph/nodes/gcore/src/logic.rs +++ b/node-graph/nodes/gcore/src/logic.rs @@ -90,6 +90,40 @@ fn string_split( string.split(&delimeter).map(str::to_string).collect() } +/// Gets a value from either a json object or array given as a string input. +/// For example, for the input {"name": "ferris"} the key "name" will return "ferris". +#[node_macro::node(category("Text"))] +fn json_get( + _: impl Ctx, + /// The json data. + data: String, + /// The key to index the object with. + key: String, +) -> String { + use serde_json::Value; + let Ok(value): Result = serde_json::from_str(&data) else { + return "Input is not valid json".into(); + }; + match value { + Value::Array(ref arr) => { + let Ok(index): Result = key.parse() else { + return "Json input is an array, but key is not a number".into(); + }; + let Some(value) = arr.get(index) else { return "Index out of bounds".into() }; + value.to_string() + } + Value::Object(map) => { + let Some(value) = map.get(&key) else { return format!("key {key} not found in object") }; + match value { + Value::String(s) => s.clone(), + Value::Number(n) => n.to_string(), + complex => complex.to_string(), + } + } + _ => "".into(), + } +} + /// Evaluates either the "If True" or "If False" input branch based on whether the input condition is true or false. #[node_macro::node(category("Math: Logic"))] async fn switch( From 90f7192edbaed5078e7f52ba1b9767f01b002cad Mon Sep 17 00:00:00 2001 From: Dennis Kobert Date: Tue, 3 Feb 2026 22:50:33 +0100 Subject: [PATCH 2/2] Return "" on error and print to console --- node-graph/nodes/gcore/src/logic.rs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/node-graph/nodes/gcore/src/logic.rs b/node-graph/nodes/gcore/src/logic.rs index 1b0f7866be..1f245b799a 100644 --- a/node-graph/nodes/gcore/src/logic.rs +++ b/node-graph/nodes/gcore/src/logic.rs @@ -107,20 +107,27 @@ fn json_get( match value { Value::Array(ref arr) => { let Ok(index): Result = key.parse() else { - return "Json input is an array, but key is not a number".into(); + log::error!("Json input is an array, but key is not a number"); + return String::new(); + }; + let Some(value) = arr.get(index) else { + log::error!("Index {} out of bounds for len {}", index, arr.len()); + return String::new(); }; - let Some(value) = arr.get(index) else { return "Index out of bounds".into() }; value.to_string() } Value::Object(map) => { - let Some(value) = map.get(&key) else { return format!("key {key} not found in object") }; + let Some(value) = map.get(&key) else { + log::error!("Key {key} not found in object"); + return String::new(); + }; match value { Value::String(s) => s.clone(), Value::Number(n) => n.to_string(), complex => complex.to_string(), } } - _ => "".into(), + _ => String::new(), } }