Search
 
SCRIPT & CODE EXAMPLE
 

SHELL

modify JSON values in place with jq

#!/bin/bash

if [ $# -lt 3 ]; then 
  echo ""
  echo "This script requires jq to be installed." 
  echo "It will add or modify the specified key and value in the JSON file."
  echo "Three arguments required: (1) key, (2) value, (3) json file to modify,"
  echo "and optionally a 4th argument s (for string) if the value is a string and should be wrapped in double quotes e.g."
  echo ""
  echo "===================================="
  echo ""
  echo "In the following examples, Modality is the key, test.json is the file"
  echo ""
  echo "To add/modify a number value, e.g.," 
  echo "jadd Modality 3 test.json"
  echo ""
  echo "To add/modify a boolean, e.g., "  
  echo "jadd Modality true test.json"
  echo ""
  echo "To add/modify an array of numbers, wrap the array in single quotes, e.g.,:"
  echo "jadd Modality '[1,3,5]' test.json"
  echo ""
  echo "To add/modify an array of strings, wrap the array in single quotes and put the strings in double quotes, e.g.,"
  echo  'jadd Modality '['"a"','"b"','"c"']' test.json'
  echo ""
  echo "To add/modify a string, add the 4th argument, s:"
  echo "jadd Modality MRI jq_test.json s"
  echo ""
  
  echo "======================="
  exit 1 
elif [ $# -eq 3 ]; then 
  key=$1
  value=$2
  file=$3
  # Add number or boolean or array using --argjson
  # echo "Three arguments key=$key; value=$value; file=$file"
  #  jq '."'"$key"'"= '$value'' $file > tmp.$$.json && mv tmp.$$.json $file
  jq --argjson val "$value" '."'"$key"'"=$val' "${file}" > tmp.$$.json && mv tmp.$$.json $file
elif [ $# -eq 4 ]; then 
  key=$1
  value=$2
  file=$3
  string=$4
  # Add a string using --arg
  # echo "Four arguments key=$key; value=$value; file=$file; string=$string"
  # jq '."'"$key"'"= "'"$value"'"' $file > tmp.$$.json && mv tmp.$$.json $file
  jq --arg val "$value" '."'"$key"'"=$val' "${file}" > tmp.$$.json && mv tmp.$$.json $file
fi
Comment

PREVIOUS NEXT
Code Example
Shell :: libSDL2_net-2.0.so.0 install on ubuntu 
Shell :: ASUS Router NVRAM script 
Shell :: The requested URL was not found on this server. Apache/2.4.29 (Ubuntu) Server at 
Shell :: linux iso terminal command 
Shell :: how to add raylib to python 
Shell :: snap remove all disabled 
Shell :: AppRegistryNotReady notebook django shell 
Shell :: npm start prevent browser 
Shell :: powershell alternative && 
Shell :: cocoapods "iphoneos" cannot be located 
Shell :: heroku upgrade phobby basic$ 
Shell :: lubuntu boot customize 
Shell :: C:Ruby30-x64in uby.exe: Is a directory -- . (LoadError) 
Shell :: change default browser is not working in manjaro 
Shell :: remove directory in linux 
Shell :: push existing repository in gthub 
Shell :: wise highlight ubuntu 
Shell :: To confirm each substitution, use the c flag: 
Shell :: preserve bash history 
Shell :: imstall prettier 
Shell :: keyboard backlight not working linux 
Shell :: yum mongodb ubuntu 16.04 
Shell :: override dh_dwz 
Shell :: download m3u8 ts files as mp4 
Shell :: powershell profile create if not exists 
Shell :: windows key not raising window in ubuntu 
Shell :: fish shell ssh agent autostart 
Shell :: cashier paddle installation 
Shell :: what is vagrant and system requirements 
Shell :: error: no se puede abrir .git/FETCH_HEAD: Permiso denegado 
ADD CONTENT
Topic
Content
Source link
Name
1+7 =