title: Loop through JSON array shell script
body: <p>I am trying to write a shell script that loops through a JSON file and does some logic based on every object's properties. The script was initially written for Windows but it does not work properly on a MacOS. </p>
  
  <p>The initial code is as follows</p>
  
  <pre><code>    documentsJson=""        
      jsonStrings=$(cat "$file" | jq -c '.[]')
  
      while IFS= read -r document; do
  
          # Get the properties from the docment (json string)
          currentKey=$(echo "$document" | jq -r '.Key')
          encrypted=$(echo "$document" | jq -r '.IsEncrypted')
  
          # If not encrypted then don't do anything with it
          if [[ $encrypted != true  ]]; then
              echoComment " Skipping '$currentKey' as it's not marked for encryption"
              documentsJson+="$document,"
  
              continue
          fi
  //some more code
  
     done &lt;&lt;&lt; $jsonStrings
  </code></pre>
  
  <p>When ran on a MacOs, the whole file is processed at once, so it does not loop through objects. 
  The closest I got to making it work - after trying a lot of suggestions - is as follows:</p>
  
  <pre><code>jq -r '.[]' "$file" | while read i; do
  
      for config in $i ; do
  
      currentKey=$(echo "$config" | jq -r '.Key')
      echo "$currentKey"
  
      done
  
  done
  </code></pre>
  
  <p>The console result is <code>parse error: Invalid numeric literal at line 1, column 6</code></p>
  
  <p>I just cannot find a proper way of grabbing the JSON object and reading its properties. </p>
  
  <p>JSON file example</p>
  
  <pre><code>[
  {
          "Key": "PdfMargins",
          "Value": {
              "Left":0,
              "Right":0,
              "Top":20,
              "Bottom":15
              }
          },
          {
          "Key": "configUrl",
          "Value": "someUrl",
          "IsEncrypted": true
      }
  ]
  
  </code></pre>
  
  <p>Thank you in advance!</p>