title: Python POST via REST API error JSONArray cannot be cast to JSONObject
body: <p>I am trying to create a python script that does the following:</p>
  
  <ul>
  <li>Parse a CSV file</li>
  <li>Send the CSV file to a remote server via REST API</li>
  </ul>
  
  <p>The code I have is working to parse the CSV file and convert it to a JSON object.</p>
  
  <p>However, when it imports to the remote server, an error message is returned: <code>org.json.simple.JSONArray cannot be cast to org.json.simple.JSONObject</code></p>
  
  <p>I think this might be because my code is creating an array of objects rather than a JSON object. How can I solve the issue so that my script will upload the CSV data and properly map the columns and values to the remote table?</p>
  
  <pre><code>#Requests package for python import requests
  import csv
  import json
  import requests
  
  f = open('example_import_csv.csv', 'rU')
  reader = csv.DictReader(f, fieldnames = ("u_date","u_product","u_serial_number"))
  out = json.dumps([row for row in reader])
  print(out) 
  
  #Set request parameters
  url = 'xxxxxx'
  user = 'xxxxxx'
  pwd = 'xxxxxxxx'
  
  #Set proper headers
  headers = {"Content-Type":"application/json","Accept":"application/json"}
  
  response = requests.post(url, auth=(user, pwd), headers=headers ,data=out)
  
  #Check for HTTP codes other than 200
  if response.status_code != 200:
      print('Status:', response.status_code, 'Headers:', response.headers, 'Response Text', response.text, 'Error Response:',response.json())
      exit()
  
  #Decode the JSON response into a dictionary and use the data
  print('Status:',response.status_code,'Headers:',response.headers,'Response:',response.json())
  </code></pre>
  
  <p>OUTPUT</p>
  
  <pre><code>[{"u_date": "u_date", "u_product": "u_product", "u_serial_number": "u_serial_number"}, {"u_date": "1/12/15", "u_product": "Apples", "u_serial_number": "11"}, {"u_date": "1/29/15", "u_product": "Pears", "u_serial_number": "12"}, {"u_date": "1/12/15", "u_product": "Oranges", "u_serial_number": "13"}, {"u_date": "1/29/15", "u_product": "Blackberries", "u_serial_number": "14"}, {"u_date": "2/5/15", "u_product": "Blueberries", "u_serial_number": "15"}, {"u_date": "2/7/15", "u_product": "Bananas", "u_serial_number": "16"}, {"u_date": "2/7/15", "u_product": "Strawberries", "u_serial_number": "17"}]
  
  Status: 200 Headers: {'Strict-Transport-Security': 'max-age=15768000; includeSubDomains;','Transfer-Encoding': 'chunked', 'Content-Encoding': 'gzip', 'Content-Type': 'application/json;charset=utf-8'}
  Response: {'reason': None, 'error': 'org.json.simple.JSONArray cannot be cast to org.json.simple.JSONObject'}
  </code></pre>
  
  <p>EDIT:</p>
  
  <p>The request must be a JSON object. I need the format to be a JSON object containing a single array.</p>
  
  <p>I tested by manually creating a json object in this format and it works. How can I parse the CSV file to get it into this particular format?</p>
  
  <pre><code>{"records":[{"u_date": "u_date", "u_product": "u_product", "u_serial_number": "u_serial_number"}, {"u_date": "1/12/15", "u_product": "Apples", "u_serial_number": "11"}, {"u_date": "1/29/15", "u_product": "Pears", "u_serial_number": "12"}, {"u_date": "1/12/15", "u_product": "Oranges", "u_serial_number": "13"}, {"u_date": "1/29/15", "u_product": "Blackberries", "u_serial_number": "14"}, {"u_date": "2/5/15", "u_product": "Blueberries", "u_serial_number": "15"}, {"u_date": "2/7/15", "u_product": "Bananas", "u_serial_number": "16"}, {"u_date": "2/7/15", "u_product": "Strawberries", "u_serial_number": "17"}]}
  </code></pre>