{"action":"created","issue":{"url":"https://api.github.com/repos/stedolan/jq/issues/1332","repository_url":"https://api.github.com/repos/stedolan/jq","labels_url":"https://api.github.com/repos/stedolan/jq/issues/1332/labels{/name}","comments_url":"https://api.github.com/repos/stedolan/jq/issues/1332/comments","events_url":"https://api.github.com/repos/stedolan/jq/issues/1332/events","html_url":"https://github.com/stedolan/jq/issues/1332","id":205711928,"number":1332,"title":"[BUG] Weird If conditional behaviour","user":{"login":"jeanpralo","id":1670389,"avatar_url":"https://avatars.githubusercontent.com/u/1670389?v=3","gravatar_id":"","url":"https://api.github.com/users/jeanpralo","html_url":"https://github.com/jeanpralo","followers_url":"https://api.github.com/users/jeanpralo/followers","following_url":"https://api.github.com/users/jeanpralo/following{/other_user}","gists_url":"https://api.github.com/users/jeanpralo/gists{/gist_id}","starred_url":"https://api.github.com/users/jeanpralo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jeanpralo/subscriptions","organizations_url":"https://api.github.com/users/jeanpralo/orgs","repos_url":"https://api.github.com/users/jeanpralo/repos","events_url":"https://api.github.com/users/jeanpralo/events{/privacy}","received_events_url":"https://api.github.com/users/jeanpralo/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2017-02-06T21:23:43Z","updated_at":"2017-02-06T21:35:45Z","closed_at":null,"body":"Hi there,\r\n\r\nI am a bit confused about the way the if condition is working.\r\n\r\nHere is what I tested:\r\n\r\n```\r\n# echo '[{\"host\": \"ch-toto\"}, {\"host\": \"toto\"}]' | jq  'if .[].host | startswith(\"ch-\") then \"yes\" else \"no\" end'\r\n\"yes\"\r\n\"no\"\r\n\r\n# echo '[{\"host\": \"ch-toto\"}, {\"host\": \"toto\"}]' | jq  'if .[].host | startswith(\"ch-\") then \"YES - \" + .[].host else \"no\" end'\r\n\"YES - ch-toto\"\r\n\"YES - toto\"\r\n\"no\"\r\n\r\n# echo '[{\"host\": \"ch-toto\"}, {\"host\": \"toto\"}]' | jq  'if .[].host | startswith(\"ch-\") then \"YES - .[].host\" else \"no\" end'\r\n\"YES - .[].host\"\r\n\"no\"\r\n\r\n\r\n\r\n# echo '[{\"host\": \"ch-toto\"}, {\"host\": \"toto\"}]' | jq  'if .[].host | startswith(\"ch-\") then \"YES - \" + .[].host else \"no - \" + .[].host end'\r\n\"YES - ch-toto\"\r\n\"YES - toto\"\r\n\"no - ch-toto\"\r\n\"no - toto\"\r\n```\r\n\r\n\r\nIt seems that every time we try to contextualize the variable we iterate every entries inside each condition.\r\n\r\nAm not using the if statement properly ?\r\n\r\n\r\n# jq  --version\r\njq-1.5-1-a5b5cbe\r\n\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/stedolan/jq/issues/comments/277821254","html_url":"https://github.com/stedolan/jq/issues/1332#issuecomment-277821254","issue_url":"https://api.github.com/repos/stedolan/jq/issues/1332","id":277821254,"user":{"login":"wtlangford","id":3422295,"avatar_url":"https://avatars.githubusercontent.com/u/3422295?v=3","gravatar_id":"","url":"https://api.github.com/users/wtlangford","html_url":"https://github.com/wtlangford","followers_url":"https://api.github.com/users/wtlangford/followers","following_url":"https://api.github.com/users/wtlangford/following{/other_user}","gists_url":"https://api.github.com/users/wtlangford/gists{/gist_id}","starred_url":"https://api.github.com/users/wtlangford/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wtlangford/subscriptions","organizations_url":"https://api.github.com/users/wtlangford/orgs","repos_url":"https://api.github.com/users/wtlangford/repos","events_url":"https://api.github.com/users/wtlangford/events{/privacy}","received_events_url":"https://api.github.com/users/wtlangford/received_events","type":"User","site_admin":false},"created_at":"2017-02-06T21:35:45Z","updated_at":"2017-02-06T21:35:45Z","body":"You're on the right track with it, though this isn't actually a bug.\r\nWhat's happening here is that the input (`.`) to the `if` expression is your list (`[{\"host\": \"ch-toto\"}, {\"host\": \"toto\"}]`).\r\nThe if expression then attempts to evaluate its conditional expression, which receives the same input (the list).\r\nThe `.[]` in the conditional expression creates a sequence of outputs, so we make a forking point here and continue the program with each successive output.\r\nFor the first output, we pass `\"ch-toto\"` to `startswith/1`, which outputs `true`.  Since the conditional evaluated to true, we now run the true-branch of the if expression, `\"YES - \" + .[].host`.  The input to this expression is still the list from before, so this outputs the sequence (because of the forking from `.[]`): `\"YES - ch-toto\", \"YES - toto\"`.  Since there's nothing else to do now, the stack unwinds back to the previous fork point in the conditional expression and continues from there, this time going down the false-branch.\r\n\r\nThis forking behavior is a bit complicated to reason about at first (and forking probably isn't the best word, but it's the word we use in the code).\r\n\r\nAt any rate, an expression like this would solve your problem: \r\n```# echo '[{\"host\": \"ch-toto\"}, {\"host\": \"toto\"}]' | jq '.[] | if .host | startswith(\"ch-\") then \"YES - \" + .host else \"no - \" + .host end'\r\n\"YES - ch-toto\"\r\n\"no - toto\"\r\n```\r\n\r\nLet me know if this is still confusing!"}}
{"actor":{"display_login":"wtlangford"}}