title: verify that a json field exists with jq and bash?
body: <p>I have a script that uses jq for parsing a json string <code>MESSAGE</code> (that is read from another application). Meanwhile the json has changed and a field is split in 2 fields: <code>file_path</code> is now split into <code>folder</code> and <code>file</code>. The script was reading the <code>file_path</code>, now the <code>folder</code> may not be present, so for creating the path of the file I have to verify if the field is there. I have search for a while on the internet, and manage to do:</p>
  
  <pre><code>echo $(echo $MESSAGE | jq .folder -r)$'/'$(echo $MESSAGE | jq .file -r)
  
  if [ $MESSAGE | jq 'has(".folder")' -r  ]
  then
          echo $(echo $MESSAGE | jq .folder -r)$'/'$(echo $MESSAGE | jq .file -r)
  else
          echo $(echo $MESSAGE | jq .file -r)
  fi
  </code></pre>
  
  <p>where <code>MESSAGE='{"folder":"FLDR","file":"fl"}'</code> or <code>MESSAGE='{"file":"fl"}'</code></p>
  
  <p>The first line is printing <code>FLDR/fl</code> or <code>null/fl</code> if the folder field is not present. So I have thought to create an if that is verifying if the folder field is present or not, but it seems that I am doing it wrong and cannot figure out what is wrong. The output is</p>
  
  <pre><code>bash: [: missing `]'
  jq: ]: No such file or directory
  null/fl
  </code></pre>