SaiKiran
SaiKiran

Reputation: 6504

Error in parsing the json and formatting to pretty json

I have used requests module and now i got the data back in json format and by taking help from this How to Python prettyprint a JSON file i have written my code and while executing the code it gave me an errro Expected a string or buffer, so i have changed the variable passed to the parser to string. Now it again gave another error.

#Import
import requests
import json

r = requests.post('http://httpbin.org/post', data = {'key':'value'})
print(r.status_code)
got_data_in_json = r.json()
parsed_json = json.loads(str(got_data_in_json))
print(json.dumps(parsed_json, indent=4 ,sort_keys=True))

Error Log:

python requests_post.py
200
Traceback (most recent call last):
  File "requests_post.py", line 8, in <module>
    parsed_json = json.loads(str(got_data_in_json))
  File "/usr/lib/python2.7/json/__init__.py", line 339, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/json/decoder.py", line 364, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python2.7/json/decoder.py", line 380, in raw_decode
    obj, end = self.scan_once(s, idx)
ValueError: Expecting property name: line 1 column 2 (char 1)

Any solutions to this problem?

Upvotes: 0

Views: 2464

Answers (3)

Vikash Singh
Vikash Singh

Reputation: 14001

r.json() returns json

so you don't need to json.loads(str(got_data_in_json))

import requests
import json

r = requests.post('http://httpbin.org/post', data = {'key':'value'})
print(r.status_code)
got_data_in_json = r.json()
print(json.dumps(got_data_in_json, indent=4 ,sort_keys=True))

output :

200
{
    "args": {},
    "data": "",
    "files": {},
    "form": {
        "key": "value"
    },
    "headers": {
        "Accept": "*/*",
        "Accept-Encoding": "gzip, deflate",
        "Content-Length": "9",
        "Content-Type": "application/x-www-form-urlencoded",
        "Host": "httpbin.org",
        "User-Agent": "python-requests/2.11.1"
    },
    "json": null,
    "origin": "103.227.98.245",
    "url": "http://httpbin.org/post"
}

Upvotes: 1

Daniel Roseman
Daniel Roseman

Reputation: 599620

r.json() parses the JSON for you and returns a Python data structure. There is no need to call json.loads() on that data.

Upvotes: 1

alecxe
alecxe

Reputation: 473873

ValueError: Expecting property name: line 1 column 2 (char 1)

The problem is, str(got_data_in_json) is not a valid JSON:

In [2]: str(got_data_in_json)
Out[2]: "{u'files': {}, u'origin': u'50.57.61.145', u'form': {u'key': u'value'}, u'url': u'http://httpbin.org/post', u'args': {}, u'headers': {u'Content-Length': u'9', u'Accept-Encoding': u'gzip, deflate', u'Accept': u'*/*', u'User-Agent': u'python-requests/2.11.1', u'Host': u'httpbin.org', u'Content-Type': u'application/x-www-form-urlencoded'}, u'json': None, u'data': u''}"

The got_data_in_json is already a Python data structure which you can dump:

got_data_in_json = r.json()
print(json.dumps(got_data_in_json, indent=4, sort_keys=True))

Upvotes: 6

Related Questions