7

I am new to python. I have PHP indexed array. Using get requests I am fetching the data.

data:

b'Array\n(\n    [0] => HTTP/1.1 301 Moved Permanently\n    [1] => Location: http://www.blahblah.com/\n    [2] => HTTP/1.1 200 OK\n    [3] => Date: Mon, 29 Oct 2018 09:29:10 GMT\n    [4] => Server: blahblah\n    [5] => Content-Length: 98836\n    [6] => Last-Modified: Mon, 29 Oct 2018 08:13:21 UTC\n    [7] => device_type: Touch\n    [8] => content-language: en\n    [9] => Set-Cookie: JSESSIONID=U-i_J9VsfqNazZl3uF4dAj2-7BmV370w58TXINO5UckAel4uGT5O!-115865558; path=/cs; HttpOnly\n    [10] => Keep-Alive: timeout=300\n    [11] => Connection: Keep-Alive\n    [12] => Content-Type: text/html; charset=UTF-8\n    [13] => Set-Cookie: NSC_q6.kjp.dpn_dmvtufs_WT.2*80=ffffffff0985006c45525d5f4f58455e445a4a422828;expires=Mon, 29-Oct-2018 09:31:10 GMT;path=/;httponly\n)\n\n' 

I Want to convert into json object. Expected output:

{
"[0]" : "HTTP/1.1 301 Moved Permanently",
"[1]" : "Location: http://www.blahblah.com/",
"[2]" : "HTTP/1.1 200 OK"
"[3]" : "Date: Mon, 29 Oct 2018 09:29:10 GMT",
......,
.....,

}

I have tried code to load json:

import requests
import json
u1="http://blahblah.com"
req=requests.get("http://gen.demotest.com/httpsProxy/httpsgetheaders.php?url=%s"%(u1))
data=req.content
print(json.dumps(data))

but got some error:

TypeError: Object of type bytes is not JSON serializable
3
  • 3
    You need to fix how this is being sent. There is no need to include all that Array\n... junk; the PHP should be posting form-encoded or JSON data. (And also note, your desired output is not JSON format but again PHP; JSON would look like {"0": "HTTP...", ...} or probably better as an array ["HTTP...", ...]). Commented Oct 29, 2018 at 9:46
  • 1
    Thanks, @DanielRoseman edited expected output Commented Oct 29, 2018 at 9:53
  • 1
    If you are getting binary as output, you have to conver to string before doing json.dumps Commented Oct 29, 2018 at 9:58

1 Answer 1

7

I think you have to convert the binary response to text. The following lines will do it.

data=json.loads(req.text)
print(data)
Sign up to request clarification or add additional context in comments.

1 Comment

I don't know why this was downvoted. This worked for me, thanks @Sushruth N!

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.