title: Converting Shopify API JSON to CSV
body: <p>Hoping that I may be able to get some assistance in regards to my code. I have spent the last couple of days crawling the site to try and find a response that would fit the requirements and while I found some excellent information, I am still struggling to implement a working version. </p>
  
  <p>I have taken my code from <a href="https://stackoverflow.com/questions/1871524/how-can-i-convert-json-to-csv">this thread</a>.</p>
  
  <p>As the title suggests I am trying to take a nested JSON file received from a Shopify Webhook, flatten it and convert it to a CSV.</p>
  
  <p>This is my JSON file:</p>
  
  <pre><code>{"id":788032119674292922,"title":"Example T-Shirt","body_html":null,"vendor":"Acme","product_type":"Shirts","created_at":null,"handle":"example-t-shirt","updated_at":null,"published_at":"2018-01-23T03:11:38-05:00","template_suffix":null,"published_scope":"global","tags":"mens t-shirt example","variants":[{"id":642667041472713922,"product_id":788032119674292922,"title":"","price":"19.99","sku":"example-shirt-s","position":0,"inventory_policy":"deny","compare_at_price":"24.99","fulfillment_service":"manual","inventory_management":null,"option1":"Small","option2":null,"option3":null,"created_at":null,"updated_at":null,"taxable":true,"barcode":null,"grams":200,"image_id":null,"inventory_quantity":75,"weight":200.0,"weight_unit":"g","inventory_item_id":null,"old_inventory_quantity":75,"requires_shipping":true},{"id":757650484644203962,"product_id":788032119674292922,"title":"","price":"19.99","sku":"example-shirt-m","position":0,"inventory_policy":"deny","compare_at_price":"24.99","fulfillment_service":"manual","inventory_management":"shopify","option1":"Medium","option2":null,"option3":null,"created_at":null,"updated_at":null,"taxable":true,"barcode":null,"grams":200,"image_id":null,"inventory_quantity":50,"weight":200.0,"weight_unit":"g","inventory_item_id":null,"old_inventory_quantity":50,"requires_shipping":true}],"options":[{"id":527050010214937811,"product_id":null,"name":"Title","position":1,"values":["Small","Medium"]}],"images":[{"id":539438707724640965,"product_id":788032119674292922,"position":0,"created_at":null,"updated_at":null,"width":323,"height":434,"src":"\/\/cdn.shopify.com\/s\/assets\/shopify_shirt-39bb555874ecaeed0a1170417d58bbcf792f7ceb56acfe758384f788710ba635.png","variant_ids":[]}],"image":null}
  </code></pre>
  
  <p>This is my code:</p>
  
  <pre><code>    import csv
      import json
  
      with open('product.json') as file:
           x = json.load(file)
          print x
  
      f = csv.writer(open("test.csv", "wb+"))
  
      f.writerow(["id", "title", "option1", "grams"])
  
      for x in x:
          f.writerow([x["id"],
                      x["title"],
                      x["variants"]["option1"],
                      x["variants"]["grams"]])
  </code></pre>
  
  <p>When running this code, I get the following error:</p>
  
  <pre><code>Traceback (most recent call last):
    File "parser5.py", line 17, in &lt;module&gt;
      f.writerow([x["id"],
   TypeError: string indices must be integers
  </code></pre>
  
  <p>I believe that the issue is to do with the fact that there is not an index reference '[0]', though I am unsure how to assign the key correctly and have failed in all of my attempts to do so. </p>
  
  <p>I am fairly new to Python, I have tried to read as many posts on the site to try not to duplicate the question - apologies if this was in vain. </p>
  
  <p>The desired output that I am looking for from the file would be something like this:</p>
  
  <pre><code>id,title,option1,grams
  788032119674292922,Example T-Shirt, Small, 200
  null,null,Medium,200
  </code></pre>
  
  <p>Any help or guidance that you may be able to offer would be greatly appreciated.</p>