G. Deward G. Deward - 3 months ago 7
JSON Question

How to retrieve JSON value in BASH (automatic version in git commit)?

How can I retrieve the

version
attribute from the following json file AND use it in a BASH script?..

file package.json

{
"name": "myapp",
"version": "0.0.1"
}


desired script: bpush.sh

#!/bin/bash
gulp bump
git add -A
eval $pkg_ver = getjson('./package.json', 'version')
git commit -a -m "$pkg_ver"
git push origin master


Obviously, the
getjson()
function is invalid.
That is what I'm trying to figure out.

Answer

If your json file is more complex then I would suggest to use jq - link

pkg_ver=$(jq '.version' package.json)

However, if you have only this two items then maybe using sed, awk or tr would be easier.