diff --git a/README.md b/README.md index 8ea7d77..c089656 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,45 @@ # json.sh -A portable shell JSON interpreter. +json.sh is a very portable JSON parser-- +it should work in just about any shell you throw at it. +Right now, it can only parse and output variables etc +in a JSON file in a more script-friendly form, but write +support will be added later. + + +Usage +------- +To use json.sh, you can simply run `json.sh` with a filename as an argument. +Here's an example command and it's output: +`sh json.sh example.json` +` +/animals +/animals/pig +/animals/pig/tail +/animals/pig/tail = curly +/animals/pig/nose +/animals/pig/nose = adorable +/animals/sheep +/animals/sheep/tail +/animals/sheep/tail = short +/animals/sheep/nose +/animals/sheep/nose = ugly +` + +There are also a few useful arguments you should keep note of: +| argument | description | +| --- | --- | +| `-v $string` | Only print variables with the name `$string` | +| `-s $string` | Only print the value of the variable `$string` | +| `-V $string` | Only print variables with the value `$string` | +| `-o $string` | Only print the object `$string` | + +With `-v` and `-s`, for example, `$string` could be anything from `tail` to `/pig/tail` to `/animals/pig/tail`. +Same syntax goes for `-o`. `-V` is the only odd one out-- it's `$string` needs to be a variable's value, like `adorable` from `/pig/nose`. + + + +Licensing +----------- +All of ST is released under the +[ISC](https://opensource.org/licenses/ISC) license. + diff --git a/json.sh b/json.sh index 51c29fa..5363dbc 100644 --- a/json.sh +++ b/json.sh @@ -66,7 +66,7 @@ do endword fi ;; - \") + "\"") if [ $NEWWORD -eq 1 ] then endword @@ -98,4 +98,56 @@ do done < "$1" } -json $1 +varmatch=0 +objmatch=0 +silmatch=0 +valmatch=0 +match='' +arguments='' +na=0 +for argument in $@ +do + case $argument in + -v) + na=1 + varmatch=1 + ;; + -V) + na=1 + valmatch=1 + ;; + -s) + na=1 + silmatch=1 + ;; + -o) + na=1 + objmatch=1 + ;; + *) + if [ $na -eq 1 ] && [ $objmatch -eq 1 ] + then + match="$argument\$" + elif [ $na -eq 1 ] && [ $varmatch -eq 1 ] + then + match="$argument\ =" + elif [ $na -eq 1 ] && [ $silmatch -eq 1 ] + then + match="$argument\ =" + elif [ $na -eq 1 ] && [ $valmatch -eq 1 ] + then + match="=\ $argument\$" + else + file="$argument" + fi + na=0 + ;; +esac +done + +if [ $silmatch -eq 1 ] +then + json $file | grep "$match" | sed 's/.*= //' +else + json $file | grep "$match" +fi