X-Git-Url: http://git.archive.openwrt.org/?p=project%2Flibubox.git;a=blobdiff_plain;f=sh%2Fjshn.sh;h=7b0155d0ce4c0ec5a4cd8438a1ecf7441f2c4868;hp=66baccbcaacff184ec25371fcdaf8159f1223dd0;hb=bb0c830b2a259b7c5ffa7c309ce9d3004f3c41a1;hpb=9c4aeda96220b3f0d097d153bedd328f71f382bf;ds=sidebyside diff --git a/sh/jshn.sh b/sh/jshn.sh index 66baccb..7b0155d 100644 --- a/sh/jshn.sh +++ b/sh/jshn.sh @@ -288,3 +288,28 @@ 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 val + + json_get_type type "$target" + case "$type" in + object|array) + local keys key + json_select "$target" + json_get_keys keys + for key in $keys; do + json_get_var val "$key" + eval "$function \"\$val\" \"\$key\" \"\$@\"" + done + json_select .. + ;; + *) + json_get_var val "$target" + eval "$function \"\$val\" \"\" \"\$@\"" + ;; + esac +}