Search code examples
jsonshjq

using a pipe symbol in a jq command throws an error


I am having a shell script file validation-example.sh , it has the followign content

expected_template = '{
  "remoteIds": [
    {
      "remoteId": "",
      "requestsReceived": 1
    },
    {
      "remoteId": "",
      "requestsReceived": 1
    },
    {
      "remoteId": "",
      "requestsReceived": 1
    },
    {
      "remoteId": "",
      "requestsReceived": 1
    }
  ]
}'

actual_json = '{
  "remoteIds": [
    {
      "remoteId": "[fd00:10:244:1:0:0:0:12]--(http://fd00-10-244-1--12.test.pod:8080)",
      "requestsReceived": 1
    },
    {
      "remoteId": "[fd00:10:244:1:0:0:0:12]--(http://fd00-10-244-1--12.test.pod:8081)",
      "requestsReceived": 1
    },
    {
      "remoteId": "[fd00:10:244:1:0:0:0:12]--(http://fd00-10-244-1--12.test.pod:8082)",
      "requestsReceived": 1
    },
    {
      "remoteId": "[fd00:10:244:1:0:0:0:12]--(http://fd00-10-244-1--12.test.pod:8083)",
      "requestsReceived": 1
    }
  ]
}'

validate_json() {
    local data="$1"
    local template="$2"
    jq -e "$data | $template" >/dev/null
}


if validate_json "$actual_json" "$expected_template"; then
    echo "The actual JSON matches the expected template."
else
    echo "The actual JSON does not match the expected template."
fi

However when i run the file it is returning the following error

validation-example.sh: line 1: expected_template: command not found
validation-example.sh: line 22: actual_json: command not found
jq: error: syntax error, unexpected '|', expecting $end (Unix shell quoting issues?) at <top-level>, line 1:
 |  
jq: 1 compile error
The actual JSON does not match the expected template.

any help please thanks


Solution

  • When shell scripting in sh, bash and similar shells, you can't put spaces around the = during assignment. If you do, it tries to find a command equal to the name of the variable you're trying to assign. You'll see this in the first two error messages that end with ": command not found".

    The next problem is that since the variables aren't assigned because of the first two errors, you're attempting to run "jq " | " which jq can't parse.

    Final note, I haven't used jq enough to encounter this problem, but with your command, it's still reading from stdin, so you don't get any output until you hit ctrl-d to signal end of file or redirect stdin. </dev/null works in unix-like OS.