0
votes

I am new to Python and reading about this it seems to be very easy but for some reason I am unable to debug the error. I am guessing it is something very simple...

2 Functions

def get_json(): return json.load(open('environment.json', 'r'))

def curlopia(j_son=get_json()): sf_url = j_son['sf_sandbox_url']['url'] grant_type = j_son['oauth_parms']['grant_type'] client_id = j_son['oauth_parms']['client_id'] client_secret = j_son['oauth_parms']['client_secret'] username = j_son['oauth_parms']['username'] password = j_son['oauth_parms']['password'] param = '-d'

I have a curl statement in a subprocess.call which returns a json string.

x=subrpocess.call(["curl", sf_url, param, "grant_type=%s" % (grant_type), param, "client_id=%s" % (client_id), param, "client_secret=%s" % (client_secret), param, "username=%s" % (username), param, "password=%s" % (password)])

or

x=os.system('curl {0} -d "grant_type={1}" -d "client_id={2}" -d "client_secret={3}" -d "username={4}" -d "password={5}" -H "X-PrettyPrint:1"'.format(sf_url, grant_type, client_id, client_secret, username, password))

When I print x the result is with trailing zero at the end.

{"id":"https://[email protected]/","issued_at":"xxxxxxxxxxx","token_type":"Bearer","instance_url":"xxxxxxxxxx","signature":"xxxxxxxxx","access_token":"xxxxxxxxxxxxx"}0

Unsure why.

when I do

json.loads(x)

gives me the below error. Also I have tried various combinations

File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/init.py", line 326, in loads return _default_decoder.decode(s)

File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 360, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end())

I am trying to understand why there is a trailing zero and if the error is related to that.If is the case can someone suggest a way around it and maybe the correct method of doing this.

Thanks

1
you are apparently trying to load invalid JSON, the zero at the end will break it. No idea what you have in curl statement. - Jan Vlcinsky

1 Answers

1
votes

You are trying to load invalid JSON document.

From your reference to curl I guess, you need to get this document by some http request.

Try getting it by using requests library.

import requests
url = "http://example.com/api"
req = requests.get(url)
assert req.ok
data = req.json()
print data

You real case might require different url, method (POST...) and possibly headers, but these you shall already know from your existing curl statement)