1

What I want to do is to get Json data out as a list (or array) with Python, I tried several times but didn't work. My json data is like this:

{
    "status":"OK",
    "List":{
        "stuff":[{
            "id":"326",
            "name":"a",
            "url":"autob-fulla.tgz",
        },{
            "id":"327",
            "name":"b",
            "url":"auto-fullb.tgz",
        },{
            "id":"328",
            "name":"c",
            "url":"auto-fullc.tgz",
        }]
    }
}

I want to return all the value of "id". Now my code is like this:

import json
def retrieve():
    print('retrieving results...')
    testQueueID = '1';
    base_url1 = 'http://localhost:8080/stuff'
    conn = Connection(base_url1, username='admin', password='admin')
    resp = conn.request_get("", args={}, headers={'content-type':'application/xml', 'accept':'application/xml'})
    decoded_json = json.loads(json.dumps(resp, sort_keys=True, indent=4, skipkeys=True))
    return decoded_json 
4
  • 1
    Your code appears to be requesting XML, yet you're talking about decoding JSON. What's going on here? Commented Jun 19, 2012 at 5:25
  • my bad, header should be: headers = {'content-type':'application/xml', 'accept':'application/json'} Commented Jun 19, 2012 at 5:26
  • In that case, you probably shouldn't be using json.dumps here - json.loads() should be all you need. Commented Jun 19, 2012 at 5:28
  • yeah, Yahoo! it works... Thank you, my friend. You are actually GOOD. Thank you, Amber. :-) Commented Jun 19, 2012 at 5:33

2 Answers 2

4

Just leave out the json.dumps call. You already have a string with JSON in it, so all you need is json.loads().

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

1 Comment

If an answer solves your problem, you should click the green checkmark to the left of it to mark it as accepted. :)
1

1.) xml != Json

2.) Try this http://www.doughellmann.com/PyMOTW/json/ first. It ll teach you.

3.) Simple code to get you started . See what data prints.

import json
from pprint import pprint
json_data=open('json_datafile')

data = json.load(json_data)
pprint(data)
json_data.close()

8 Comments

Thank you, Vrashabh. Lemme try this one.
I tried:json_data=open('parameter.json') data = json.load(json_data)----the last line gets error
ret = conn.request_post("/bynewbuild/", body=content, headers=headers) abc = ret[u'body'] json_data=open(abc) in this case, the error is: "IOError: [Errno 2] No such file or directory: u'{"......."
Then I did this: json_data=open('parameter.json') data = json.load(json_data) File "D:\workspaces\TestPythonProject\src\MyTest.py", line 50, in callRESTschedulerAPI data = json.load(json_data) File "C:\Python27\lib\json_init_.py", line 278, in load **kw) File "C:\Python27\lib\json_init_.py", line 326, in loads return _default_decoder.decode(s) File "C:\Python27\lib\json\decoder.py", line 366, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "C:\Python27\lib\json\decoder.py", line 382, in raw_decode obj, end =
Sorry, Lafada. I guess I'm giving you a headache. I still couldn't figure out how to post code in comments. Hope you would understand what I posted in here. Still, I want more solution on this. Thanks in advance.
|

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.