1

I am sending a json post request in Python and I am getting two return dictionary values but they are stored in one object.

This is what I have:

import json
import requests

def call():
    pay = {'token' : "4593543"}
    r = requests.post('https://www.hackerrank.com/challenges', params=pay)

I am getting two values store in the r object.

'{"needle":"qvsmbjkc", "haystack": ["pgunkqay","qvsmbjkc","qgswmzin","arwokfjm","taskzcup","yjvcabgr","xcrsldof","tecipvzf","cjtahlqb","pqgykrcz","ufyjrpad","hqezmcwl","fsyimbxr","tosqznha","lzujpvob","mbfsikde","nqvpjbhi","uwsqybai","ozetipqw","imancdqr"]}'

A needle and haystack value but they are both in the r object. How can I separate them into different variables?

3
  • Your question is badly formatetd. Edit the question, select the code in it, click the {} button to format the code as code. Use the preview below the textbox to see what it it will look like. Commented Oct 31, 2016 at 8:37
  • You should be calling the r.json() method to extract the JSON object . Commented Oct 31, 2016 at 8:41
  • Thank you, so I got the needle string to be stored in a variable but when I stored the array in a variable, it stores it as an entire string instead of an index of strings. Is there a way I can fix that? Commented Oct 31, 2016 at 19:43

1 Answer 1

1

requests as a json-Method to read json responses directly into python structures:

r = requests.post('https://www.hackerrank.com/challenges', params=pay)
response = r.json()
print(response['needle'])
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, so I got the needle string to be stored in a variable but when I stored the array in a variable, it stores it as an entire string instead of an index of strings. Is there a way I can fix that?
@Jack: Have you tried? No, response['haystack'] is a list of strings.

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.