Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env bash
- jsonLoadFromStdIn="json.load(sys.stdin)"
- loopOverItems=false
- loopJsonOperation=""
- __doJsonOperation() {
- json="$1"
- operation="$2"
- operationWithJsonLoad=${operation/__JSON__/$jsonLoadFromStdIn}
- echo "${json}" | python -c "import sys, json; print ${operationWithJsonLoad}"
- }
- __convertJsonPathToOperation() {
- jsonPath="$1"
- jsonPath="${jsonPath:1}"
- jsonPath="${jsonPath//[/.[}"
- IFS='.' read -ra jsonPathParts <<< "${jsonPath}"
- loopOverItems=false
- loopJsonOperation=""
- jsonOperation="__JSON__"
- for jsonPathPart in "${jsonPathParts[@]}"; do
- if [ "${jsonPathPart:0:1}" = "[" ] && [ "${jsonPathPart: -1}" = "]" ]; then
- # handle array lookups
- if [ "${jsonPathPart}" = "[*]" ]; then
- # act on all items in array
- loopOverItems=true
- else
- # get item at index in array
- if [ "${loopOverItems}" = false ]; then
- jsonOperation="${jsonOperation}${jsonPathPart}"
- else
- loopJsonOperation="${loopJsonOperation}${jsonPathPart}"
- fi
- fi
- elif [ "${jsonPathPart}" = "length()" ]; then
- # get length of array
- if [ "${loopOverItems}" = false ]; then
- jsonOperation="len(${jsonOperation})"
- else
- loopJsonOperation="${loopJsonOperation}.__len__()"
- fi
- elif [ ! -z "${jsonPathPart}" ]; then
- # get object property
- if [ "${loopOverItems}" = false ]; then
- jsonOperation="${jsonOperation}['${jsonPathPart}']"
- else
- loopJsonOperation="['${loopJsonOperation}${jsonPathPart}']"
- fi
- fi
- done
- echo "jsonOperation=\"${jsonOperation}\"; loopOverItems=${loopOverItems}; loopJsonOperation=\"${loopJsonOperation}\""
- }
- json-path-query() {
- json="$1"
- jsonPath="$2"
- if [ -z "${json}" ] || [ -z "${jsonPath}" ]; then
- >&2 echo "Usage: $0 json jsonPath"
- return 1
- fi
- eval $(__convertJsonPathToOperation "${jsonPath}")
- if [ "${loopOverItems}" = false ]; then
- __doJsonOperation "${json}" "${jsonOperation}"
- fi
- numItems="$(__doJsonOperation "${json}" "len(${jsonOperation})")"
- for ((i=0; i < ${numItems}; i++)); do
- __doJsonOperation "${json}" "${jsonOperation}[${i}]${loopJsonOperation}"
- done
- }
Add Comment
Please, Sign In to add comment