From 31a28850334fb26bd6e8a61f057ea5b72c294627 Mon Sep 17 00:00:00 2001 From: Matthias Kruk Date: Mon, 5 Jul 2021 08:01:01 +0900 Subject: [PATCH] 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. --- include/json.sh | 14 ++++++++++++++ 1 file changed, 14 insertions(+) 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=("$@") -- 2.47.3