From 03700cdbc2c3018b8da0a95e494d6f46c8dbed78 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Wed, 24 Jun 2020 16:36:33 +0200 Subject: [PATCH] array2Json convert newline to \n in case string contains newline --- modules/jsonDB.sh | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/modules/jsonDB.sh b/modules/jsonDB.sh index 6cd621b..289843e 100644 --- a/modules/jsonDB.sh +++ b/modules/jsonDB.sh @@ -5,7 +5,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-70-g694ee61 +#### $$VERSION$$ v0.98-dev-74-g3d760b3 # # source from commands.sh to use jsonDB functions # @@ -339,11 +339,13 @@ Json2Array() { # $1 ARRAY name, must be declared with "declare -A ARRAY" before calling Array2Json() { [ -z "$1" ] && return 1 - local key + local key val declare -n ARRAY="$1" for key in "${!ARRAY[@]}" do - printf '["%s"]\t"%s"\n' "${key//,/\",\"}" "${ARRAY[${key}]//\"/\\\"}" + # in case val contains newline convert to \n + val="${ARRAY[${key}]//$'\n'/\\n}" + printf '["%s"]\t"%s"\n' "${key//,/\",\"}" "${val//\"/\\\"}" done }