1

I'm working with the Mega API and Python in hope to produce a folder tree readable by Python. At the moment I'm working with the JSON responses Mega's API gives, but for some reason am having trouble parsing it. In the past I would simply use simplejson in the format below, though right now it's not working. At the moment I'm just trying to get the file name. Any help is appreciated!

import simplejson    

megaResponseToFileSearch = "(u'BMExefXbYa', {u'a': {u'n': u'A Bullet For Pretty Boy - 1 - Dial M For Murder.mp3'}, u'h': u'BMExXbYa', u'k': (5710166, 21957970, 11015946, 7749654L), u'ts': 13736999, 'iv': (7949460, 15946811, 0, 0), u'p': u'4FlnwBTb', u's': 5236864, 'meta_mac': (529642, 2979591L), u'u': u'xpz_tb-YDUg', u't': 0, 'key': (223xx15874, 642xx8505, 1571620, 26489769L, 799460, 1596811, 559642, 279591L)})"

jsonRespone = simplejson.loads(megaResponseToFileSearch)

print jsonRespone[u'a'][u'n']

ERROR:

Traceback (most recent call last):
  File "D:/Projects/Mega Sync/megasync.py", line 18, in <module>
    jsonRespone = simplejson.loads(file4)
  File "D:\Projects\Mega Sync\simplejson\__init__.py", line 453, in loads
    return _default_decoder.decode(s)
  File "D:\Projects\Mega Sync\simplejson\decoder.py", line 429, in decode
    obj, end = self.raw_decode(s)
  File "D:\Projects\Mega Sync\simplejson\decoder.py", line 451, in raw_decode
    raise JSONDecodeError("No JSON object could be decoded", s, idx)
simplejson.decoder.JSONDecodeError: No JSON object could be decoded: line 1 column 0 (char 0)

EDIT:

I was asked where I got the string from. It's a response to searching for a file using the Mega API. I'm using the module found here. https://github.com/richardasaurus/mega.py The code itself looks like this:

from mega import Mega
mega = Mega({'verbose': True})
m = mega.login(email, password)
file = m.find('A Bullet For Pretty Boy - 1 - Dial M For Murder.mp3')
print file
2
  • That doesn't look like a valid json string. Where did you get it (post the code if possible)? Commented Jul 10, 2013 at 18:41
  • Updated question with code. Was it too quick of me to assume it was JSON it was spitting out? Commented Jul 10, 2013 at 18:45

1 Answer 1

1

The thing you are getting from m.find is just a python tuple, where the 1-st (next after the 0th) element is a dictionary:

(u'99M1Tazb',
 {u'a': {u'n': u'test.txt'}, 
  u'h': u'99M1Tazb', 
  u'k': (1145485578, 1435138417, 702505527, 274874292), 
  u'ts': 1373482712,
  'iv': (1883603069, 763415510, 0, 0), 
  u'p': u'9td12YaY', 
  u's': 0, 
  'meta_mac': (1091379956, 402442960),
  u'u': u'79_166PAQCA', 
  u't': 0,
  'key': (872626551, 2013967015, 1758609603, 127858020, 1883603069, 763415510, 1091379956, 402442960)})

To get the filename, just use:

print file[1]['a']['n'] 

So, no need to use simplejson at all.

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

1 Comment

AH! That's why I got mixed up. Thank you very much!

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.