Skip to content

Commit 4056fde

Browse files
authored
fix curly brackets in strings for bash json parser (#868)
1 parent a0b4e6b commit 4056fde

1 file changed

Lines changed: 31 additions & 14 deletions

File tree

src/main/resources/io/viash/languages/bash/ViashParseJson.sh

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,38 @@ function ViashParseJsonBash {
4040
nested_json+="$line"
4141

4242
# Count braces to track when nested object ends
43-
if [[ "$line" =~ \{ ]]; then
44-
((nested_depth++)) || true
45-
fi
46-
if [[ "$line" =~ ^\}[[:space:]]*,?[[:space:]]*$ ]]; then
47-
((nested_depth--)) || true
48-
if [ $nested_depth -eq 0 ]; then
49-
# Nested object complete - store as JSON string (bash 3.2 compatible)
50-
# Use printf %q to safely escape for eval (handles backticks correctly in bash 3.2)
51-
printf -v _viash_escaped '%q' "$nested_json"
52-
eval "${nested_name}=$_viash_escaped"
53-
in_nested=false
54-
nested_name=""
55-
nested_json=""
43+
# Must ignore braces inside quoted strings
44+
local in_quotes=false
45+
local prev_char=""
46+
for ((i=0; i<${#line}; i++)); do
47+
local char="${line:$i:1}"
48+
49+
# Track if we're inside a quoted string (ignore escaped quotes)
50+
if [ "$char" = '"' ] && [ "$prev_char" != '\' ]; then
51+
in_quotes=$( [ "$in_quotes" = "true" ] && echo "false" || echo "true" )
5652
fi
57-
fi
53+
54+
# Only count braces outside of quoted strings
55+
if [ "$in_quotes" = "false" ]; then
56+
if [ "$char" = '{' ]; then
57+
((nested_depth++)) || true
58+
elif [ "$char" = '}' ]; then
59+
((nested_depth--)) || true
60+
if [ $nested_depth -eq 0 ]; then
61+
# Nested object complete - store as JSON string (bash 3.2 compatible)
62+
# Use printf %q to safely escape for eval (handles backticks correctly in bash 3.2)
63+
printf -v _viash_escaped '%q' "$nested_json"
64+
eval "${nested_name}=$_viash_escaped"
65+
in_nested=false
66+
nested_name=""
67+
nested_json=""
68+
break
69+
fi
70+
fi
71+
fi
72+
73+
prev_char="$char"
74+
done
5875
continue
5976
fi
6077

0 commit comments

Comments
 (0)