array2Json convert newline to \n in case string contains newline

This commit is contained in:
Kay Marquardt (Gnadelwartz) 2020-06-24 16:36:33 +02:00
parent 3d760b3319
commit 03700cdbc2
1 changed files with 5 additions and 3 deletions

View File

@ -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
}