title: Why is Python treating my dict as str?
body: <p>This is my JSON:</p>
  
  <blockquote>
  <pre><code>json = {"url":"https://www.amazon.com.au/Cenovis-Multivitamin-Minerals-Tablets-Value/dp/B07D2M5QKN/ref=br_asw_pdt-5","title":"Cenovis Multivitamin and Minerals Tablets Value Pack 200: Amazon.com.au: Health &amp; Personal Care","additionalData":{"locality":{"country":"US","language":"en"}},"statusCode":"200","items":[{"@type":"WebPage","description":"Cenovis Multivitamin and Minerals Tablets Value Pack 200: Amazon.com.au: Health &amp; Personal Care","name":"Cenovis Multivitamin and Minerals Tablets Value Pack 200: Amazon.com.au: Health &amp; Personal Care","mainEntity":[{"@type":"Product","category":"Health &amp; Household","name":"Cenovis Multivitamin and Minerals Tablets Value Pack 200","offers":{"@type":"Offer","availability":"InStock","price":9.8,"priceCurrency":"USD"},"image":"https://images-na.ssl-images-amazon.com/images/I/51qhXMVf3LL.jpg","additionalProperty":{"@type":"PropertyValue","name":"productFeatures","value":["Maintain memory, mood and cognitive function in the elderly (or with ageing), Maintain healthy bones and joints","No added yeast, gluten, lactose, sugar, artificial colours or flavours, artificial sweeteners, or dairy products.","Cenovis Multivitamin and Mineral contains 19 specially selected ingredients to provide daily nutritional support.","Assist your body with energy production","Support healthy immune function"]}}]}]}
  </code></pre>
  </blockquote>
  
  <p>When I do a Python type check i.e.:</p>
  
  <pre><code>type(json) ... Output = dict
  </code></pre>
  
  <p>Then I try to use dict.items I get the below error:</p>
  
  <p><strong>AttributeError: 'str' object has no attribute 'items'</strong> (Python type is evaluating it as Dict, but no idea why its getting treated as Str when I try to use the .items)</p>
  
  <p>I tried ast.literal_eval also, but no luck.</p>
  
  <p><strong>The same code works fine for the below JSON:</strong></p>
  
  <blockquote>
  <pre><code>json = {"url":"https://www.tarocash.com.au/au/navy-bahamas-slim-shirt-171ls101","title":"Navy Bahamas Slim Stretch Shirt | Men's Apparel | Tarocash","favIcon":"https://cdn.tarocash.com.au/media/favicon/stores/5/favicon.png","additionalData":{"locality":{"country":"AU","language":"en"}},"statusCode":"200","items":[{"@type":"WebPage","description":"Navy Bahamas Slim Stretch Shirt available online at Tarocash. Shop Tarocash's smart casual men's clothes for special occasions, work and your weekend.","image":"https://tarocash.imgix.net/Tarocash/Products/171LS101_NVY_CROP.png","name":"BAHAMAS SLIM STRETCH SHIRT","mainEntity":[{"@type":"Product","offers":[{"@type":"Offer","availability":"InStock","priceCurrency":"AUD","price":89.99},{"@type":"Offer","availability":"InStock","priceCurrency":"AUD","price":89.99}],"name":"NAVY BAHAMAS SLIM STRETCH SHIRT"},{"@type":"SocialMediaAccounts","facebookPage":"tarocash","instagram":"tarocash"}]}]}
  </code></pre>
  </blockquote>
  
  <p>I'm using Python 3.7 on Windows 10 (64 bit)</p>
  
  <p>My full code below:</p>
  
  <pre><code>from flask import Flask
  import requests
  import json
  import sys
  import ast
  
  app = Flask(__name__)
  
  @app.route('/')
  def getData():
  
      request_data = [
          ('url','https://www.amazon.com.au/dp/B078GH9T4R/ref=ods_bn_cat_aucc_h3_dot'),
      ]
      r = requests.get('https://&lt;&lt;My_API_Server&gt;&gt;', auth=('XYZ@test.com','&lt;&lt;my_valid_key&gt;&gt;'), data=request_data)
  
      result = {}
  
      j = (r.content).decode('utf-8')
  
      print(j, file=sys.stderr)
  
      d = json.dumps(j)
      d1 = ast.literal_eval(d)
      data = json.loads(d1)
  
      if type(data) is dict:
          print("**** ITS A DICT ***", file=sys.stderr)
      else:
          print("**** ITS A STRING ***", file=sys.stderr)
  
      print("**********************", file=sys.stderr)
      print(type(data), file=sys.stderr)
      print(data, file=sys.stderr)
      print("**********************", file=sys.stderr)
  
      result['url']    = return_item(list(find('url', data)))
  
      print(return_item(list(find('url', data))), file=sys.stderr)
  
      result['title']  = return_item(list(find('title', data)))
      result['name']   = return_item(list(find('name', data))[0].split('|'))
      result['image']  = return_item(list(find('image', data)))
      result['current_price'] = return_item(list(find('price', data)))
  
      return json.dumps(result)
  
  
  #####################################################################    
  def find(key, dictionary):
      for k, v in dictionary.items():
          if k == key:
              yield v
          elif isinstance(v, dict):
              for result in find(key, v):
                  yield result
          elif isinstance(v, list):
              for d in v:
                  for result in find(key, d):
                      yield result
  
  def return_item(list):
      if len(list) &gt; 0:
          return list[0]
      else:
          return "Not_Available"
  
  
  
  #    print(list_values[0].get('mainEntity'),file=sys.stderr)
  </code></pre>
  
  <p>Full Error below:</p>
  
  <pre><code>&gt; [2018-08-10 11:40:33,463] ERROR in app: Exception on / [GET]
  Traceback (most recent call last):
    File "&lt;&lt;My_Local_dev_Path&gt;&gt;", line 2292, in wsgi_app
      response = self.full_dispatch_request()
    File "&lt;&lt;My_Local_dev_Path&gt;&gt;", line 1815, in full_dispatch_request
      rv = self.handle_user_exception(e)
    File "&lt;&lt;My_Local_dev_Path&gt;&gt;", line 1718, in handle_user_exception
      reraise(exc_type, exc_value, tb)
    File "&lt;&lt;My_Local_dev_Path&gt;&gt;", line 35, in reraise
      raise value
    File "&lt;&lt;My_Local_dev_Path&gt;&gt;", line 1813, in full_dispatch_request
      rv = self.dispatch_request()
    File "&lt;&lt;My_Local_dev_Path&gt;&gt;", line 1799, in dispatch_request
      return self.view_functions[rule.endpoint](**req.view_args)
    File "&lt;&lt;My_Local_dev_Path&gt;&gt;", line 37, in getData
      result['url']    = return_item(list(find('url', data)))
    File "&lt;&lt;My_Local_dev_Path&gt;&gt;", line 60, in find
      for result in find(key, d):
    File "&lt;&lt;My_Local_dev_Path&gt;&gt;", line 60, in find
      for result in find(key, d):
    File "&lt;&lt;My_Local_dev_Path&gt;&gt;", line 60, in find
      for result in find(key, d):
    File "&lt;&lt;My_Local_dev_Path&gt;&gt;", line 52, in find
      for k, v in dictionary.items():
  AttributeError: 'str' object has no attribute 'items'
  </code></pre>