]> git.corax.cc Git - toolbox/commitdiff
include/json: Fix parsing of output from jq in json_array_tail()
authorMatthias Kruk <m@m10k.eu>
Sat, 7 Jan 2023 08:33:53 +0000 (17:33 +0900)
committerMatthias Kruk <m@m10k.eu>
Sat, 7 Jan 2023 08:37:37 +0000 (17:37 +0900)
The `json_array_tail()' function does not correctly handle return
values and parse output from jq, causing it to incorrectly handle
arrays that contain arrays or objects.

This commit modifies `json_array_tail()' so that it correctly
recognizes the type of data read from jq.

include/json.sh
test/json_spec.sh

index 862747ac11f3c3b6c8ee2dfd1e7a34ed2bfaedf8..832116c564c4bd321f48c33875a0b934aab3095f 100644 (file)
@@ -261,12 +261,18 @@ json_array_tail() {
        local tail
        local element
        local new
+       local string_re
 
+       string_re='^"(.*)"$'
        tail=()
 
        while read -r element; do
-               tail+=("$element")
-       done < <(jq -r '.[1:][]' <<< "$array")
+               if [[ "$element" =~ $string_re ]]; then
+                       tail+=("${BASH_REMATCH[1]}")
+               else
+                       tail+=("$element")
+               fi
+       done < <(jq -c '.[1:][]' <<< "$array")
 
        if ! new=$(json_array "${tail[@]}"); then
                return 1
index bbe5e0d534bfb6232a3fb004ec5e481917fd3c63..0fe5f292f5ac569479dbfcd47e6ed31330f43c01 100644 (file)
@@ -339,8 +339,8 @@ Describe "json_array_tail()"
   End
 
   It "removes an object from the head of the array"
-    When call json_array_tail '[{}, {"hello": "world"}]'
-    The stdout should equal '[{"hello": "world"}]'
+    When call json_array_tail '[{}, {"hello":"world"}]'
+    The stdout should equal '[{"hello":"world"}]'
     The status should equal 0
   End
 End
@@ -352,7 +352,7 @@ Describe "json_array_to_lines()"
   End
 
   It "splits a string array to lines"
-    When call json_array_to_lines '["hello", "world"]'
+    When call json_array_to_lines '["hello","world"]'
     The first line of stdout should equal "hello"
     The second line of stdout should equal "world"
     The status should equal 0