JSONRPC Query to Python DICT. - Printable Version +- Kodi Community Forum (https://forum.kodi.tv) +-- Forum: Development (https://forum.kodi.tv/forumdisplay.php?fid=32) +--- Forum: Add-ons (https://forum.kodi.tv/forumdisplay.php?fid=26) +--- Thread: JSONRPC Query to Python DICT. (/showthread.php?tid=100598) |
JSONRPC Query to Python DICT. - giftie - 2011-05-02 With the HTTP API lifespan coming to an end soon(true it may be Post Eden) I thought I might supply a small bit of code that I use for getting the results from a JSON RPC query into a useful Python DICT. Nuka had provided the general code, I just made it work for my needs. Code: def retrieve_json_dict(json_query, items='items', force_log=False ): For instance to get all the file paths for an album(using the database id): Code: json_query = '{"jsonrpc": "2.0", "method": "AudioLibrary.GetSongs", "params": {"albumid": 30, "fields": ["file"] }, "id": 1}' The full JSONRPC response may look like this: Code: { Notice that the Info we want is after "songs": hence the Code: items="songs" The procedure will create a DICT(also know as a Hash) similar to the following: Code: [{'songid': 362, 'file': '/media/freenas/MP3 Music/Full Albums/ACDC/Highway To Hell/01 - Highway to Hell.flac', 'label': 'Highway to Hell'}, {'songid': 363, 'file': '/media/freenas/MP3 Music/Full Albums/ACDC/Highway To Hell/02 - Girls Got Rhythm.flac', 'label': 'Girls Got Rhythm'}, ...... }] Which is a form that is really easy to use in Python. - topfs2 - 2011-05-02 Wonder if the executeJSONRPC perhaps should take a dict and return a dict? I opted for the lazy version when I implemented the python thing. - giftie - 2011-05-03 topfs2 Wrote:Wonder if the executeJSONRPC perhaps should take a dict and return a dict? I opted for the lazy version when I implemented the python thing. I don't feel it needs to change. Being in this form allows the command to be more versatile. It took me a little while to figure out the proper forming of a Query(thanks to the Simple REST Client in Google Chrome). - hippojay - 2011-05-04 I've just been using a json.loads() to convert my responses to dicts. Is using eval() any better or quicker? - giftie - 2011-05-05 hippojay Wrote:I've just been using a json.loads() to convert my responses to dicts. Is using eval() any better or quicker? Off hand I'm not sure... Didn't know about json.loads(), so I just tried it and haven't been successful putting the response from executeJSONRPC() into it(errors out, though eval() motors through the same response) Once I figure out how to get the response from executeJSONRPC() I can time it.. and put up a comparisim between the two... for instance, using the following query: Code: {"jsonrpc": "2.0", "method": "VideoLibrary.GetMovies", "params": {"fields": ["title", "thumbnail", "file", "plot", "plotoutline", "rating", "runtime", "director", "genre", "mpaa", "votes", "trailer", "tagline", "top250", "studio", "year", "writer", "streamDetails" ] }, "id": 1} it took eval() 0.690 seconds to change to a DICT. ( 1083 movies ) |