Numeric argument passed with jq --arg not matching data with ==

前端 未结 2 1655
一整个雨季
一整个雨季 2020-12-18 06:10

Here is a sample JSON response from my curl:

{
  \"success\": true,
  \"message\": \"jobStatus\",
  \"jobStatus\": [
    {
      \"ID\": 9,
      \"status\":         


        
2条回答
  •  春和景丽
    2020-12-18 06:38

    Assuming you want to check for the JSON value 2, you have a choice to make - either convert the argument of --arg to a number, or use --argjson with a numeric argument. These alternatives are illustrated by the following:

    jq --arg v 2 '.jobStatus[] | select(.ID == ($v|tonumber) | .status' 
    
    jq --argjson v 2 '.jobStatus[] | select(.ID == $v) | .status'
    

    Note that --argjson requires a relatively recent version of jq.

    Of course, if you want to "normalize" .ID so that it's always treated as a string, you could write:

    jq --arg v 2 '.jobStatus[] | select((.ID|tostring) == $v) | .status'
    

提交回复
热议问题