From: Matthias Kruk Date: Sun, 4 Jul 2021 23:01:01 +0000 (+0900) Subject: include/json: Add json_object_get() function X-Git-Url: https://git.corax.cc/?a=commitdiff_plain;h=31a28850334fb26bd6e8a61f057ea5b72c294627;p=toolbox include/json: Add json_object_get() function 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. --- diff --git a/include/json.sh b/include/json.sh index 606b15c..b9b0469 100644 --- a/include/json.sh +++ b/include/json.sh @@ -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=("$@")