title: update json with jq through shell script
body: <p>I created a shell script to create and update json files using jq. To create json files is working well. I have a variable that is passed as argument to jq command</p>
  
  <pre><code>#!/bin/sh
  OPER=$1
  FILE_PATH=$2
  DATE_TIME=`date +%Y-%m-%d:%H:%M:%S`
  DATE=`date +%Y-%m-%d`
  CSV=$3
  STEP=$4
  STATUS=$5
  CODE=$6
  MESSAGE=$7
  
  if [ "$#" -eq 7 ]; then
      if [ "$OPER" == "create" ]; then
          # echo "FILE_PATH: $FILE_PATH - CSV: $CSV - STEP: $STEP - STATUS: $STATUS - CODE: $CODE - MESSAGE: $MESSAGE"
          REPORT="{\"date\": \"$DATE\", \"csv\": \"$CSV\", \"messages\": [{ \"timestamp\": \"$DATE_TIME\", \"step\": \"$STEP\", \"status\": \"$STATUS\", \"code\": \"$CODE\", \"message\": \"$MESSAGE\" }] }"
          echo ${REPORT} | jq . &gt; $FILE_PATH
      elif [ "$OPER" == "update" ]; then
                  echo "FILE_PATH: $FILE_PATH - CSV: $CSV - STEP: $STEP - STATUS: $STATUS - CODE: $CODE - MESSAGE: $MESSAGE"
          REPORT="{\"timestamp\": \"$DATE_TIME\", \"step\": \"$STEP\", \"status\": \"$STATUS\", \"code\": \"$CODE\", \"message\": \"$MESSAGE\"}"
          echo "REPORTTTTT: "$REPORT
          REPORT="jq '.messages[.messages| length] |= . + $REPORT' $FILE_PATH"
          #echo $REPORT
          echo `jq '.messages[.messages| length] |= . + $REPORT' $FILE_PATH` &gt; $FILE_PATH
      else
          echo "operation not recognized: $OPER"
      fi
  else
          echo "wrong parameters."
  fi
  jq . $FILE_PATH
  </code></pre>
  
  <p>But to update the json file I am getting an error. My $REPORT variable is correct. The cotes are correct. I think I must use another jq argument instead of <code>|= . +</code>. I used that command with plain text and it worked. But when I create the REPORT variable dynamically it throws an error.</p>
  
  <p>Any clue? Thanks</p>
  
  <pre><code>REPORTTTTT: {"timestamp": "2017-02-17:12:11:11", "step": "2", "status": "OK", "code": "34", "message": "message 34 file.xml"}
  jq: error: syntax error, unexpected INVALID_CHARACTER, expecting $end (Unix shell quoting issues?) at &lt;top-level&gt;, line 1:
  '.messages[.messages|
  jq: 1 compile error
  
  jq: error: REPORT/0 is not defined at &lt;top-level&gt;, line 1:
  .messages[.messages| length] |= . + $REPORT                                    
  jq: 1 compile error
  </code></pre>
  
  <p>Here is the example at the command line>></p>
  
  <pre><code>$ jq . file.json 
  {
    "date": "2017-02-17",
    "csv": "file.csv",
    "messages": [
      {
        "timestamp": "2017-02-17:12:31:21",
        "step": "1",
        "status": "OK",
        "code": "33",
        "message": "message 33"
      }
    ]
  }
  $ export REPORT="{\"timestamp\": \"2017-02-17:11:51:14\", \"step\": \"2\", \"status\": \"OK\", \"code\": \"34\", \"message\": \"message 34 file.xml\"}"
  $ echo $REPORT
  {"timestamp": "2017-02-17:11:51:14", "step": "2", "status": "OK", "code": "34", "message": "message 34 file.xml"}
  $ jq '.messages[.messages| length] |= . + $REPORT' file.json 
  jq: error: REPORT/0 is not defined at &lt;top-level&gt;, line 1:
  .messages[.messages| length] |= . + $REPORT                                    
  jq: 1 compile error
  </code></pre>