1

I am trying to write a script to download images from an API, I have a set up a loop that is as follows:

    response = requests.get(url, params=query)
    json_data = json.dumps(response.text)
    pythonVal = json.loads(json.loads(json_data))
    print(pythonVal)

The print(pythonVal) returns:

 {
  "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",

I keep getting an error that reads:

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

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

TypeError: string indices must be integers

Ultimately I'd like to be able to get data from it by doing something like

 pythonVal['data']['_embedded']['uploader']['username'] 

Thanks for your input!

0

2 Answers 2

1

Why doing json.loads() twice? Change:

json.loads(json.loads(json_data))

to:

json.loads(json_data)

and it should work.

Now since you are getting error TypeError: string indices must be integers on doing pythonVal['data'], it means that the value of pythonVal is of list type and not dict. Instead do:

for item in pythonVal:
    print item

Please also mention the sample JSON content with the question, if you want better help from others :)

Sign up to request clarification or add additional context in comments.

2 Comments

Hey thanks! So I was doing json.loads(json_data) but as I mentioned I was getting that second error, if I do for item in pythonVal, it will just print each individual letter in from my response, which is not what I want, I am trying to get specific data from an API response code"status": "approved", "likes": 23, "request_id": null,code for example, If I wanted to get the status I'd want to be able to say pythonVal['status'] and have it return 'approved'
I order to let me better understand your problem, please could you update the question with the output of print pythonVal and in that point me to the value you need.
0

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

# -*- coding: utf-8 -*-

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.