]> git.corax.cc Git - toolbox/commitdiff
include/json: Add json_object_get() function
authorMatthias Kruk <m@m10k.eu>
Sun, 4 Jul 2021 23:01:01 +0000 (08:01 +0900)
committerMatthias Kruk <m@m10k.eu>
Sun, 4 Jul 2021 23:01:01 +0000 (08:01 +0900)
The json module doesn't provide any functions to retrieve data from
within JSON types, requiring users to call jq (or other parsers)
directly.
This commit adds the json_object_get() function, which allows the user
to retrieve the value of a field in a JSON object.

include/json.sh

index 606b15c15239a1dc2a7c695d01777134429595e4..b9b0469bfb6e3b452e4afaa93c1e668b362a5f0c 100644 (file)
@@ -80,6 +80,20 @@ json_object() {
         return 0
 }
 
+json_object_get() {
+       local object="$1"
+       local field="$2"
+
+       local value
+
+       if ! value=$(jq -e -r ".$field" <<< "$object"); then
+               return 1
+       fi
+
+       echo "$value"
+       return 0
+}
+
 json_array() {
        local args=("$@")