title: How convert numpy array to JSON for Google Cloud ML?
body: <p>I have a numpy array (<code>X_test</code>) for testing my model in <code>cloud-ml</code>. For online prediction it is necessary to convert it to JSON format.</p>
  
  <p>My numpy array has the next format:</p>
  
  <pre><code>[[    0     0     0 ...  7464  1951  2861]
   [    0     0     0 ...  3395  1996  4999]
   [    0     0     0 ...  5294  9202 17867]
   ...
   [    0     0     0 ...  3506   977  7818]
   [    0     0     0 ...  1421    75   137]
   [    0     0     0 ... 12857 12686  2928]]
  </code></pre>
  
  <p>I use the next code for converting it to JSON:</p>
  
  <pre><code>import json
  b = X_test.tolist()
  json_file = "file.json" 
  json.dump(b, codecs.open(json_file, 'w', encoding='utf-8'), sort_keys=True, indent=4)
  </code></pre>
  
  <p>After this I use <code>Google Cloud SDK Shell</code> for cloud prediction and enter the next command:</p>
  
  <p><code>gcloud ml-engine predict --model keras_model --version v1 --json-instances file.json</code></p>
  
  <p>However, I get the next error:</p>
  
  <p><code>ERROR: (gcloud.ml-engine.predict) Input instances are not in JSON format. See "gcloud ml-engine predict --help" for details.</code></p>
  
  <p>As I understood, I incorrectly converted numpy to JSON for cloud-ml. </p>
  
  <p>How correctly to convert numpy into JSON for avoiding this error?</p>
  
  <hr>
  
  <p><strong>UPD</strong>: Here's the code that helped me solve this problem:</p>
  
  <pre><code>import json
  b = X_test.tolist()
  json_file = "file.json"
  
  with open(json_file, 'w', encoding='utf-8') as f:
      for i in b:
          instance = {"input": i}
          json.dump(instance, f , sort_keys=True)
          f.write("\n")
  </code></pre>