0

After contacting a server I get the following strings as response

{"kind": "t2", "data": {"has_mail": null, "name": "shadyabhi", "created": 1273919273.0, "created_utc": 1273919273.0, "link_karma": 1343, "comment_karma": 301, "is_gold": false, "is_mod": false, "id": "425zf", "has_mod_mail": null}}

which is stored as type 'str' in my script.

Now, when I try to decode it using json.dumps(mystring, sort_keys=True, indent=4), I get this.

"{\"kind\": \"t2\", \"data\": {\"has_mail\": null, \"name\": \"shadyabhi\", \"created\": 1273919273.0, \"created_utc\": 1273919273.0, \"link_karma\": 1343, \"comment_karma\": 301, \"is_gold\": false, \"is_mod\": false, \"id\": \"425zf\", \"has_mod_mail\": null}}"

which should really be like this

shadyabhi@archlinux ~ $ echo '{"kind": "t2", "data": {"has_mail": "null", "name": "shadyabhi", "created": 1273919273.0, "created_utc": 1273919273.0, "link_karma": 1343, "comment_karma": 299, "is_gold": "false", "is_mod": "false", "id": "425zf", "has_mod_mail": "null"}}' | python2 -mjson.tool
{
    "data": {
        "comment_karma": 299, 
        "created": 1273919273.0, 
        "created_utc": 1273919273.0, 
        "has_mail": "null", 
        "has_mod_mail": "null", 
        "id": "425zf", 
        "is_gold": "false", 
        "is_mod": "false", 
        "link_karma": 1343, 
        "name": "shadyabhi"
    }, 
    "kind": "t2"
}
shadyabhi@archlinux ~ $

So, what is it that's going wrong?

4 Answers 4

2

You need to load it before you can dump it. Try this:

data = json.loads(returnFromWebService)

json.dumps(data, sort_keys=True, indent=4)

To add a bit more detail - you're receiving a string, and then asking the json library to dump it to a string. That doesn't make a great deal of sense. What you need to do first is put the data into a more meaningful container. By calling loads you take the string value of the return and parse it into an actual Python Dictionary. Then, you can pass that data to dumps which outputs a string using your requested formatting.

1
  • Aha, while looking at the documentation, I was wondering as to how to get that obj. Now, I know. So, by using loads, I deserialize the string.
    – shadyabhi
    Sep 13, 2011 at 17:17
1

You have things backwards. If you want to convert a string to a data structure you need to use json.loads(thestring). json.dumps() is for converting a data structure to a json encoded string.

1

You are supposed to dump an object (like a dictionary) which then becomes a string, not the other way round... see here.

Use json.loads() instead.

1

You want json.loads. The dumps method is for going the other way (dumping an object to a json string).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.