0

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. Commented Apr 29, 2014 at 5:37

1 Answer 1

1

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)

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your response. You are right about the invalid JSON. When I use the same curl statement in the terminal I get a valid JSON. An invalid JSON is returned from only the python script and I am able to see it when I print the result returned from a subprocess.call or os.system. I have also checked the encoding of the .json file and tried both UTF-8 and UTF-16. Unfortunately both yield the same results. I have updated the initial question to reflect the curl statement. Please have a look and let me know.
With the updates to question if you reckon I have to post a new question I will do that. Cheers
Although this is not what I did to fix the issue, I am agreeing to the response.

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.