Access JSON data from API
<p>I am trying to write a script to download images from an API, I have a set up a loop that is as follows: </p>

<pre><code>    response = requests.get(url, params=query)
    json_data = json.dumps(response.text)
    pythonVal = json.loads(json.loads(json_data))
    print(pythonVal)
</code></pre>

<p>The print(pythonVal) returns: </p>

<pre><code> {
  "metadata": {
    "code": 200,
    "message": "OK",
    "version": "v2.0"
  },
  "data": {
  "_links": {
    "self": {
      "href": "redactedLink"
    }
  },
  "id": "123456789",
  "_fixed": true
    ,
  "type": "IMAGE",
  "source": "social media",
  "source_id": "1234567890_1234567890",
  "original_source": "link",
  "caption": "caption",
  "video_url": null,
    "share_url": "link",
    "date_submitted": "2016-07-11T09:34:35+00:00",
  "date_published": "2016-09-11T16:30:26+00:00",
</code></pre>

<p>I keep getting an error that reads:</p>

<pre><code>UnicodeEncodeError: 'ascii' codec can't encode character '\xc4' in
position 527: ordinal not in range(128)
</code></pre>

<p>For the pythonVal variable, if I just have it set to <code>json.loads(json_data)</code>, it prints out the JSON response, but then when I try doing <code>pythonVal['data']</code> I get another error that reads: </p>

<pre><code>TypeError: string indices must be integers
</code></pre>

<p>Ultimately I'd like to be able to get data from it by doing something like</p>

<pre><code> pythonVal['data']['_embedded']['uploader']['username'] 
</code></pre>

<p>Thanks for your input! </p>

<p>Put the following on top of your code. This works by overriding the native ascii encoding of Python to UTF-8. </p>

<pre><code># -*- coding: utf-8 -*-
</code></pre>

<p>The second error is because you have already gotten the string, and you need integer indices to get the characters of the string. </p>

python json