[PY] Best way to read JSON response?
#1
I'd like your opinion on the most efficient way to handle a json response in py. All I'm trying to do is use JSON to get the XBMC movie IDs for a series of IMDB numbers, but in the absence of any JSON filters I need to dump the whole thing to a string (quite quick) then turn it into a massive searchable tuple (very slow). JSON is my alternative to using sql to connect to the db - I quite like not being dependent on db type or location.

Code:
import re, simplejson, xbmc, jsonrpclib
jsonurl='{"jsonrpc": "2.0", "method": "VideoLibrary.GetMovies","params": {"properties" : ["tag","imdbnumber"], "sort": { "order": "ascending", "method": "label", "ignorearticle": true } }, "id": "libMovies"}'
jsonresponse=simplejson.loads(xbmc.executeJSONRPC(jsonurl))
xbmcdbtuple = re.findall(r'imdbnumber\': u\'(.+?)\', u\'tag\': \[(.+?)\]\', u\'movieid\': (.+?), u\'label\': \'(.+?)\'', data)

This cannot be the smartest way to go about things - ideas?
Reply
#2
You can use the json module
PHP Code:
import json

# python to json
python_dict = {'foo'1}
json_str json.dumps(python_dict)

# json to python
json_str '{"foo": 1}'
python_dict json.loads(json_str

For an example of using it together with XBMC's jsonRPC see here:
PHP Code:
def get_xbmc_movies():
    
import json
    query 
= {
        
'jsonrpc''2.0',
        
'id'0,
        
'method''VideoLibrary.GetMovies',
        
'params': {
            
'properties': ['imdbnumber''file']
        }
    }
    
response json.loads(xbmc.executeJSONRPC(json.dumps(query)))
    
movie_dict dict(
        (
movie['imdbnumber'], movie['file'])
        for 
movie in response.get('result', {}).get('movies', [])
    )
    return 
movie_dict 
My GitHub. My Add-ons:
Image
Reply
#3
Can't quite make it do what my json command above is doing. Why doesn't this work?
Code:
# python to json
   python_dict = {'foo': 1}
   json_str = json.dumps(python_dict)
# json to python
   json_str = '{"foo": 1}'
   python_dict = json.loads(json_str)
   def get_xbmc_movies():
    import json
    query = {
        'jsonrpc': '2.0',
        'method': 'VideoLibrary.GetMovies',
        'params': {
            'properties': ['imdbnumber', 'tag'],
        'sort': { 'order': 'ascending', 'method': 'label' }, 'id': 'libMovies'        
        }
    }
    response = json.loads(xbmc.executeJSONRPC(json.dumps(query)))
    movie_dict = dict(
        (movie['imdbnumber'], movie['tag'], movie['label'], movie['id'])
        for movie in response.get('result', {}).get('movies', [])
    )
    return movie_dict  

   print get_xbmc_movies()
Reply
#4
in the meantime, this hacky regex does the trick. But I like your method more and still want to understand how to use it.

Code:
jsonurl='{"jsonrpc": "2.0", "method": "VideoLibrary.GetMovies","params": {"properties" : ["tag","imdbnumber"], "sort": { "order": "ascending", "method": "label", "ignorearticle": true } }, "id": "libMovies"}'
   jsonresponse=simplejson.loads(xbmc.executeJSONRPC(jsonurl))
   xbmcdbtuple = re.findall(r'u\'imdbnumber\': u\'(tt.+?)\', u\'tag\': (.+?), u\'movieid\': (.+?), u\'label\': u[\'|"](.+?)[\'|"]', str(jsonresponse))
   print xbmcdbtuple
Reply
#5
some more JSON examples
https://github.com/XBMC-Addons/script.ar...a_setup.py
slightly different way. might give you some pointers you need
Read/follow the forum rules.
For troubleshooting and bug reporting, read this first
Interested in seeing some YouTube videos about Kodi? Go here and subscribe
Reply
#6
Is that your actual code wellspokenman? If so, the formatting looks off. Can you clarify a little more what you mean by "[it] doesn't work?"
Reply
#7
the hacky regex is the actual code - that does what I want (fetches XBMC ID, imdb ID, tag and label). It puts it into a massive tuple and I loop through it with 'for' statements. Spheres code is obviously much better - my initial code response was my attempt to get the fields I need using his approach, but that is completely broken because I don't understand that way of doing JSON queries.
Reply
#8
If you trust the source JSON is basically a python dict.

PHP Code:
# JSON uses true, false and null
null None
true 
True
false 
False

result 
= eval(json_source
For python coding questions first see http://mirrors.xbmc.org/docs/python-docs/
Reply
#9
(2013-04-30, 19:53)Martijn Wrote: some more JSON examples
https://github.com/XBMC-Addons/script.ar...a_setup.py
slightly different way. might give you some pointers you need

Confused by this:

Code:
# Use json instead of simplejson when python v2.7 or greater
if sys.version_info < (2, 7):
    import json as simplejson
else:
    import simplejson

Which is correct - the code or the comment? The code is using simplejson when python is 2.7 or greater, but the comment says it should be using json when python is 2.7 or greater. I'm assuming the code is correct...
Texture Cache Maintenance Utility: Preload your texture cache for optimal UI performance. Remotely manage media libraries. Purge unused artwork to free up space. Find missing media. Configurable QA check to highlight metadata issues. Aid in diagnosis of library and cache related problems.
Reply
#10
no it imports json and names it simplejson
that way you can keep using the same functions

PHP Code:
# Use json instead of simplejson when python v2.7 or greater
if sys.version_info < (27):
    
import json as some_import
else:
    
import simplejson as some_import 
that way you can call some_import.loads() for example.

The reason why we did this was that we found out that this gives the best performance after some testing.
Read/follow the forum rules.
For troubleshooting and bug reporting, read this first
Interested in seeing some YouTube videos about Kodi? Go here and subscribe
Reply
#11
(2013-05-01, 20:42)Martijn Wrote: no it imports json and names it simplejson
that way you can keep using the same functions

I see what the code is doing, just that it conflicts with the comment.
Texture Cache Maintenance Utility: Preload your texture cache for optimal UI performance. Remotely manage media libraries. Purge unused artwork to free up space. Find missing media. Configurable QA check to highlight metadata issues. Aid in diagnosis of library and cache related problems.
Reply
#12
(2013-05-01, 20:43)MilhouseVH Wrote:
(2013-05-01, 20:42)Martijn Wrote: no it imports json and names it simplejson
that way you can keep using the same functions

I see what the code is doing, just that it conflicts with the comment.
hmm indeed.
comment is correct. code isn't. guess i forgot to fix that after some push
Read/follow the forum rules.
For troubleshooting and bug reporting, read this first
Interested in seeing some YouTube videos about Kodi? Go here and subscribe
Reply
#13
Code:
#Use json instead of simplejson when python v2.7 or greater

So it should be executing
Code:
import json as simplejson

when sys.version_info >= (2, 7), but that isn't what the code is doing.

So the comment doesn't correspond with the code, and vice versa. It's a small matter, I'm sure the code is correct, just that the comment is confusing as a result...
Texture Cache Maintenance Utility: Preload your texture cache for optimal UI performance. Remotely manage media libraries. Purge unused artwork to free up space. Find missing media. Configurable QA check to highlight metadata issues. Aid in diagnosis of library and cache related problems.
Reply
#14
yes that's what i said
seems i forgot to push that fix.
other scripts use it like it should
https://github.com/XBMC-Addons/service.x...son.py#L29
Read/follow the forum rules.
For troubleshooting and bug reporting, read this first
Interested in seeing some YouTube videos about Kodi? Go here and subscribe
Reply
#15
No need to thank me. Rolleyes
Texture Cache Maintenance Utility: Preload your texture cache for optimal UI performance. Remotely manage media libraries. Purge unused artwork to free up space. Find missing media. Configurable QA check to highlight metadata issues. Aid in diagnosis of library and cache related problems.
Reply

Logout Mark Read Team Forum Stats Members Help
[PY] Best way to read JSON response?0