How do I process this using Python?

2013-08-31 Thread Anthony Papillion
Hello Everyone,

I'm writing a processor for Bitmessage messages and I am needing to
parse the following returned JSON string:

{u'inboxMessages': [{u'fromAddress':
u'BM-2DBYkhiBZCyrBa8J7gFRGrFRSGqtHgPtMvwQ', u'toAddress':
u'BM-2DC7SCTj2gzgrGgMvUCARdrfrsgLyz3iMyN3', u'read': 0, u'msgid':
u'36659a4453e12a085d8fbfeefc58da8fb23f38bfb0984c2983e0ddc31c776038',
u'receivedTime': u'1377986524', u'message':
u'dGVzdGluZyAxIDIgMw0KDQotLQ0KSm9obiBQZXJyeQ0KDQo=\n', u'encodingType':
2, u'subject': u'bWVzc2FnZSAx\n'}, {u'fromAddress':
u'BM-2DBYkhiBZCyrBa8J7gNBrngtgttHgPtMvwQ', u'toAddress':
u'BM-2DC7SCTj2gzgrGgMvUCARdCrfthyz3iMyN3', u'read': 0, u'msgid':
u'2ebe10c788ed47c6c122e3b43ae6642cb15077536c7056ed5088ab2d339c4630',
u'receivedTime': u'1377986557', u'message':
u'VGhpcyBpcyB0aGUgbmV4dCB0ZXN0DQoNCi0tDQpKb2huIFBlcnJ5DQoNCg==\n',
u'encodingType': 2, u'subject': u'dGVzdGluZyAzIDQgNQ==\n'},
{u'fromAddress': u'BM-2DBYkhithgyhyrBa8J7gNBrnSGqtHgPtMvwQ',
u'toAddress': u'BM-2DC7SCTj2gzgrtgtgMvUCARdCogLyz3iMyN3', u'read': 0,
u'msgid':
u'91dffd421c898aab0ffc43a363869a580abec6fa851aa6cf7cefe98263f96c81',
u'receivedTime': u'1377986599', u'message':
u'VGhpcyBpcyB0aGUgM3JkIHRlc3QNCg0hjj0NCkpvaG4gUGVycnkNCg0K\n',
u'encodingType': 2, u'subject': u'dGhpcyBpcyB0aGUgM3Jk\n'}]}

I tried using the following code:

data = json.loads(api.getAllInboxMessages) # This is the API call

for messageSender in data['inboxMessages']['fromAddress']
print messageSender

For some reason (probably obvious reasons) isn't working. I'm trying to
loop through the JSON and return all of the fromAddress fields.

Can anyone offer suggestions?

Thanks,
Anthony
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How do I process this using Python?

2013-08-31 Thread Chris Angelico
On Sun, Sep 1, 2013 at 9:44 AM, Anthony Papillion papill...@gmail.com wrote:
 I'm writing a processor for Bitmessage messages and I am needing to
 parse the following returned JSON string:

 {u'inboxMessages':

Does the JSON string really have those u prefixes and apostrophes?
That's not valid JSON. You may be able to use ast.literal_eval() on it
- I was able to with the example data - but not a JSON parser. Can you
sort out your transmission end?

ChrisA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How do I process this using Python?

2013-08-31 Thread Anthony Papillion
On 08/31/2013 06:48 PM, Chris Angelico wrote:
 On Sun, Sep 1, 2013 at 9:44 AM, Anthony Papillion papill...@gmail.com wrote:
 I'm writing a processor for Bitmessage messages and I am needing to
 parse the following returned JSON string:

 {u'inboxMessages':
 
 Does the JSON string really have those u prefixes and apostrophes?
 That's not valid JSON. You may be able to use ast.literal_eval() on it
 - I was able to with the example data - but not a JSON parser. Can you
 sort out your transmission end?
 
 ChrisA

I think I remembered what the 'u' prefix is. It indicates that the data
following is a unicode string. So could that be valid JSON data wrapped
up in unicode?

Anthony


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How do I process this using Python?

2013-08-31 Thread Chris Angelico
On Sun, Sep 1, 2013 at 10:19 AM, Anthony Papillion papill...@gmail.com wrote:
 On 08/31/2013 06:48 PM, Chris Angelico wrote:
 On Sun, Sep 1, 2013 at 9:44 AM, Anthony Papillion papill...@gmail.com 
 wrote:
 I'm writing a processor for Bitmessage messages and I am needing to
 parse the following returned JSON string:

 {u'inboxMessages':

 Does the JSON string really have those u prefixes and apostrophes?
 That's not valid JSON. You may be able to use ast.literal_eval() on it
 - I was able to with the example data - but not a JSON parser. Can you
 sort out your transmission end?

 ChrisA

 I think I remembered what the 'u' prefix is. It indicates that the data
 following is a unicode string. So could that be valid JSON data wrapped
 up in unicode?

No; JSON already supports Unicode. What you may have is an incorrect
JSON encoder that uses Python's repr() to shortcut its work - but it's
wrong JSON.

But bitmessage seems to be written in Python. Can you simply access
the objects it's giving you, rather than going via text strings?

ChrisA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How do I process this using Python?

2013-08-31 Thread Cameron Simpson
On 31Aug2013 19:19, Anthony Papillion papill...@gmail.com wrote:
| On 08/31/2013 06:48 PM, Chris Angelico wrote:
|  On Sun, Sep 1, 2013 at 9:44 AM, Anthony Papillion papill...@gmail.com 
wrote:
|  I'm writing a processor for Bitmessage messages and I am needing to
|  parse the following returned JSON string:
| 
|  {u'inboxMessages':
|  
|  Does the JSON string really have those u prefixes and apostrophes?
|  That's not valid JSON. You may be able to use ast.literal_eval() on it
|  - I was able to with the example data - but not a JSON parser. Can you
|  sort out your transmission end?
| 
| I think I remembered what the 'u' prefix is. It indicates that the data
| following is a unicode string. So could that be valid JSON data wrapped
| up in unicode?

How sure are you that it is JSON? It looks to me like a message
that might once have been JSON, but has already been passed through
json.loads() for you.

What does type(the_json) say? What does repr(the_json) say?
Print both.

I would guess it has already been parsed.

Cheers,
-- 
Cameron Simpson c...@zip.com.au

Sattinger's Second Law: Even though you've plugged it in, it still won't work
until you switch it on.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How do I process this using Python?

2013-08-31 Thread Anthony Papillion
On 08/31/2013 07:32 PM, Cameron Simpson wrote:
 On 31Aug2013 19:19, Anthony Papillion papill...@gmail.com wrote:
 | On 08/31/2013 06:48 PM, Chris Angelico wrote:
 |  On Sun, Sep 1, 2013 at 9:44 AM, Anthony Papillion papill...@gmail.com 
 wrote:
 |  I'm writing a processor for Bitmessage messages and I am needing to
 |  parse the following returned JSON string:
 | 
 |  {u'inboxMessages':
 |  
 |  Does the JSON string really have those u prefixes and apostrophes?
 |  That's not valid JSON. You may be able to use ast.literal_eval() on it
 |  - I was able to with the example data - but not a JSON parser. Can you
 |  sort out your transmission end?
 | 
 | I think I remembered what the 'u' prefix is. It indicates that the data
 | following is a unicode string. So could that be valid JSON data wrapped
 | up in unicode?
 
 How sure are you that it is JSON? It looks to me like a message
 that might once have been JSON, but has already been passed through
 json.loads() for you.
 
 What does type(the_json) say? What does repr(the_json) say?
 Print both.
 
 I would guess it has already been parsed.
 
 Cheers,

And you would be right! It's actually a dictionary!

Anthony


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How do I process this using Python? (SOLVED)

2013-08-31 Thread Anthony Papillion
On 08/31/2013 07:32 PM, Chris Angelico wrote:
 On Sun, Sep 1, 2013 at 10:19 AM, Anthony Papillion papill...@gmail.com 
 wrote:
 On 08/31/2013 06:48 PM, Chris Angelico wrote:
 On Sun, Sep 1, 2013 at 9:44 AM, Anthony Papillion papill...@gmail.com 
 wrote:
 I'm writing a processor for Bitmessage messages and I am needing to
 parse the following returned JSON string:

 {u'inboxMessages':

 Does the JSON string really have those u prefixes and apostrophes?
 That's not valid JSON. You may be able to use ast.literal_eval() on it
 - I was able to with the example data - but not a JSON parser. Can you
 sort out your transmission end?

 ChrisA

 I think I remembered what the 'u' prefix is. It indicates that the data
 following is a unicode string. So could that be valid JSON data wrapped
 up in unicode?
 
 No; JSON already supports Unicode. What you may have is an incorrect
 JSON encoder that uses Python's repr() to shortcut its work - but it's
 wrong JSON.
 
 But bitmessage seems to be written in Python. Can you simply access
 the objects it's giving you, rather than going via text strings?
 
 ChrisA

Once I looked at the data in a better structured way and saw it in the
proper type it became clear. Here is my solution:

data = json.loads(api.getAllInboxMessages())
for message in data['inboxmessages']:
print message['fromAddress']

Yep, it was that simple. Thanks for the help guys! I appreciate how fast
everyone responded.

Anthony


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How do I process this using Python?

2013-08-31 Thread Steven D'Aprano
On Sat, 31 Aug 2013 18:44:04 -0500, Anthony Papillion wrote:

 Hello Everyone,
 
 I'm writing a processor for Bitmessage messages and I am needing to
 parse the following returned JSON string:
[...]
 For some reason (probably obvious reasons) isn't working. I'm trying to
 loop through the JSON and return all of the fromAddress fields.
 
 Can anyone offer suggestions?


Yes. Don't assume that isn't working is obvious. What do you mean 
isn't working? What part isn't working? What does it do? Does it print 
the wrong results, or does it raise an exception? If so, please copy and 
paste the entire traceback, do not simplify it, summarise it, or retype 
it from memory.


-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list