dross dross - 1 year ago 89
JSON Question

Passing bash variable to python command within a shell script

Needing to extract two variables per line from a JSON file and use each of these 2 variables in separate follow up commands.

My script is so far:

#!/bin/bash

VCTRL_OUT='/tmp/workingfile.json'

old_IFS=$IFS # save the field separator
IFS=$'\n' # new field separator, the end of line

for line in $(cat $VCTRL_OUT)
do
python -c 'import json,sys;obj=json.load($line);print obj["ip_range"]'
done


The second to last line is wrong and need to know how to do this.

The following works:

cat /tmp/workingfile.json | head -n +1 | python -c 'import json,sys;obj=json.load(sys.stdin);print obj["ip_range"]';


But not sure how to do the same in the bash script for loop.

Answer Source

Python wouldn't be my first choice for this kind of one-liner, but you might try

#!/bin/bash

VCTRL_OUT='/tmp/workingfile.json'

parse_json () {
    python -c $'import json,fileinput\nfor line in fileinput.input(): print json.loads(line)["ip_range"]' "$1"
}
while IFS= read -r ip_range; do
   # do your thing with ip_range
done < <(parse_json "$VCTRL_OUT")

One alternative is to replace the Python bit with jq:

while IFS= read -r ip_range; do
  # ...
done < <( jq -rR 'fromjson | .ip_range' "$VCTRL_OUT")

Another alternative is to replace the entire bash script with Python, although that may be easier said than done, depending on what the bash script is actually doing.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download