]> git.plutz.net Git - cgilite/commitdiff
get json values via jpath
authorPaul Hänsch <paul@plutz.net>
Sun, 25 Feb 2024 15:49:44 +0000 (16:49 +0100)
committerPaul Hänsch <paul@plutz.net>
Sun, 25 Feb 2024 15:49:44 +0000 (16:49 +0100)
json.sh

diff --git a/json.sh b/json.sh
index 99e1e746140af0d635080941e04d089d28043af5..43026bed6bae6a5f9197b61cb2d75f2fd13eb278 100755 (executable)
--- a/json.sh
+++ b/json.sh
@@ -5,6 +5,8 @@
 
 . "${_EXEC:-.}/cgilite/db23.sh"
 
+debug(){ [ $# -gt 0 ] && printf '%s\n' "$@" >&2 || tee -a /dev/stderr; }
+
 json_except() {
   printf '%s\n' "$@" >&2;
   printf 'Exc: %s\n' "$json_document" >&2
@@ -94,6 +96,7 @@ json_number() {
 
 json_value() {
   local value json_document="$json_document"
+  json_type=""
 
   json_space
   case $json_document in
@@ -101,33 +104,40 @@ json_value() {
       value="$(json_string)" || return 1
       json_document="${value#* }"
       value="str:${value%%     *}"
+      json_type=string
       ;;
     [+-.0-9]*)
       value="$(json_number)" || return 1
       json_document="${value#* }"
       value="${value%% *}"
+      json_type=number
       ;;
     "{"*)
       value="$(json_object)" || return 1
       json_document="${value#* }"
       value="obj:${value%%     *}"
+      json_type=object
       ;;
     "["*)
       value="$(json_array)" || return 1
       json_document="${value#* }"
       value="arr:${value%%     *}"
+      json_type=array
       ;;
     null*)
       json_document="${json_document#null}"
       value="null"
+      json_type=null
       ;;
     true*)
       json_document="${json_document#true}"
       value="true"
+      json_type=boolean
       ;;
     false*)
       json_document="${json_document#false}"
       value="false"
+      json_type=boolean
       ;;
   esac
 
@@ -234,3 +244,56 @@ json_load() {
 
   json_value
 }
+
+json_get() {
+  local json="$1" jpath="${2#.}" id idx
+  json_type=''
+
+  case $json in
+    str:*) json_type="string";;
+    arr:*) json_type="array";;
+    obj:*) json_type="object";;
+    num:*) json_type="number";;
+    true|false)
+           json_type="boolean";;
+    null)  json_type="null";;
+  esac
+  json="${json#???:}"
+
+  case $jpath in
+    "")
+      printf %s\\n "$json"
+      return 0
+      ;;
+    "["[0-9]*"]"*)
+      idx="${jpath%%"]"*}" idx="${idx#"["}"
+      jpath="${jpath#"["*"]"}"
+      ;;
+    "['"*"']"*)
+      id="${jpath%%"']"*}" id="${id#"['"}"
+      jpath="${jpath#"['"*"']"}"
+      ;;
+    *) id="${jpath%%[".["]*}"
+      jpath="${jpath#"$id"}"
+      ;;
+  esac
+
+  if   [  "$id" -a "$json_type" = object ]; then
+    # if ! json="$(DB2 "$(UNSTRING "$json")" get "$id")"; then
+    if ! json="$(DB2 "$json" get "$id")"; then
+      debug "No key: \"$id\""
+      return 1
+    fi
+  elif [ "$idx" -a "$json_type" = array ]; then
+    if ! json="$(DB2 "$(UNSTRING "$json")" get @ "$(( idx + 1 ))")"; then
+    # if ! json="$(DB2 "$json" get @ "$(( idx + 1 ))")"; then
+      debug "No array index: \"$idx\""
+      return 1
+    fi
+  else
+    debug "Value type missmatch"
+    return 1
+  fi
+  json_get "$json" "$jpath"
+  return $?
+}