Function usefull to iterate through the different elements of an
array or object; the provided callback function is called for each
value.
For field types different from array or object the callback is called
with the retrieved value.

Signed-off-by: Hans Dedecker <dedec...@gmail.com>
---
 sh/jshn.sh | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)

diff --git a/sh/jshn.sh b/sh/jshn.sh
index bf76edb..aa2e868 100644
--- a/sh/jshn.sh
+++ b/sh/jshn.sh
@@ -278,3 +278,30 @@ json_is_a() {
        json_get_type type "$1"
        [ "$type" = "$2" ]
 }
+
+json_for_each_item() {
+       [ "$#" -ge 2 ] || return 0
+       local function="$1"; shift
+       local target="$1"; shift
+       local type
+       local val
+
+       json_get_type type "$target"
+       case "$type" in
+               object|array)
+                       local keys
+                       local key
+                       json_select "$target"
+                       json_get_keys keys
+                       for key in $keys; do
+                               json_get_var val "$key"
+                               eval "$function \"\$val\" \"\$@\""
+                       done
+                       json_select ..
+               ;;
+               *)
+                       json_get_var val "$target"
+                       eval "$function \"\$val\" \"\$@\""
+               ;;
+       esac
+}
-- 
2.15.1


_______________________________________________
Lede-dev mailing list
Lede-dev@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/lede-dev

Reply via email to