Demetrius
Demetrius

Reputation: 451

Storing a json post request in variables in python

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?

Upvotes: 1

Views: 4878

Answers (1)

Daniel
Daniel

Reputation: 42778

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'])

Upvotes: 1

Related Questions