Samuel Muiruri
Samuel Muiruri

Reputation: 635

Json format for python

I'm rewriting a view based on what I know the final output should be in json but it's returning the dictionary as a string.

new output

{
    "results": 
    ["
    {
        'plot': u'', 
        'runtime': u'N/A', 
        'description': u'x', 
        'videos': [
        {
            'id': 823, 
            'name': u'x', 
            'youtube_id': u'FtcubOnXgZk'
        }
        ], 
        'country': u'India', 
        'writer': u'Neetu Varma, Ranjeev Verma', 
        'name': u'Chalk N Duster', 
        'id': 940, 
        'director': u'Jayant Gilatar', 
        'hot': True, 
        'content': u'x', 
        'actors': u'Shabana Azmi, Arya Babbar, Gavie Chahal, Juhi Chawla', 
        'year': 2015, 
        'images': [
        {'small': '/media/cache/62/fd/62fd5158d281c042e3cf1f919183e94e.jpg', 'medium': '/media/cache/5e/32/5e32ebb1a4d25bba0d0c70b4b448e948.jpg'}], 
        'trailer_youtube_id': u'FtcubOnXgZk', 
        'type': 'movie', 
        'slug': u'chalk-n-duster', 
        'categories': [{'parent_id': 2, 'id': 226, 'name': u'Drama'}], 
        'shows': {
            'starts': '2016-01-16', 
            'booking_url': u'', 
            'venue': {
                'address': u'', 
                'id': 854, 
                'name': u'Nyali Cinemax', 
                'area': {
                    'id': 52, 
                    'parent': {
                        'id': 48, 
                        'name': u'Mombasa'
                        }, 
                    'name': u'Nyali'
                    }
                }, 
                'starts_time': '18:30:00'
                }
            }", "{'plot': u'' ....

old output

"results": [
        {
            "actors": "x", 
            "categories": [
                {
                    "id": 299, 
                    "name": "Biography", 
                    "parent_id": 2
                }, 
            ], 
            "content": "x", 
            "country": "x", 
            "description": "x", 
            "director": "x", 
            "hot": true, 
            "id": 912, 
            "images": [
                {
                    "medium": "/media/cache/d2/b3/d2b3a7885e7c39bfc5c2b297b66619c5.jpg", 
                    "small": "/media/cache/e2/d0/e2d01b2c7c77d3590536666de4a7fd7d.jpg"
                }
            ], 
            "name": "Bridge of Spies", 
            "plot": "x", 
            "runtime": "141 min", 
            "shows": [
                {
                    "booking_url": "", 
                    "starts": "2015-11-27", 
                    "starts_time": "16:30:00", 
                    "venue": {
                        "address": "The Junction Shopping Mall", 
                        "area": {
                            "id": 68, 
                            "name": "Ngong Road", 
                            "parent": {
                                "id": 2, 
                                "name": "Nairobi"
                            }
                        }, 
                        "id": 1631, 
                        "name": "Century Cinemax Junction"
                    }
                }, 
            ], 
            "slug": "bridge-of-spies", 
            "trailer_youtube_id": "", 
            "type": "movie", 
            "videos": [
                {
                    "id": "795", 
                    "name": "Bridge of Spies", 
                    "youtube_id": "2-2x3r1m2I4"
                }
            ], 
            "writer": "Matt Charman, Ethan Coen, Joel Coen", 
            "year": 2015
        }, ...

        ]

Here's the view, I know the shows should also be a list, but in order to start testing I'll need the data to come in the right format. If it's involves too much rewriting I'm okay with links and explanation.

@memoize(timeout=60*60)
def movies_json():
    today = datetime.date.today()
    movies = Movie.objects.filter(shows__starts__gte=today)
    results = []
    number = len(movies)
    for movie in movies:
        print "Now Remaining: {0}".format(number)
        number -= 1
        medium = get_thumbnail(movie.picture(), '185x274', crop='center', quality=99).url
        small = get_thumbnail(movie.picture(), '50x74', crop='center', quality=99).url
        movie_details = {
            'director':movie.director,
            'plot':movie.plot,
            'actors':movie.actors,
            'content':movie.content,
            'country':movie.country,
            'description':movie.description,
            'hot':movie.hot,
            'id':movie.id,
            'images':[{'medium':medium, 'small':small}],
            'name':movie.name,
            'plot':movie.plot,
            'runtime':movie.runtime,
            'slug':movie.slug,
            'type':'movie',
            'writer':movie.writer,
            'year':movie.year,

        }
        youtube_details = movie.videos.filter(youtube_id__isnull=False)[0]
        movie_details['trailer_youtube_id'] = youtube_details.youtube_id if youtube_details.youtube_id else ""
        movie_details['videos'] = [
            {
                'id':youtube_details.id,
                'name':movie.name,
                'youtube_id':youtube_details.youtube_id,
            }
        ]
        shows = []
        for show in movie.shows.all():
            show_details = {
                'booking_url':show.booking_url,
                'starts':show.starts.isoformat(),
                'starts_time':show.starts_time.isoformat(),
                'venue': {
                    'address':show.venue.address,
                    'area': {
                        'id': show.venue.area.id,
                        'name': show.venue.area.name,
                        'parent': {
                            'id': show.venue.area.parent.id,
                            'name': show.venue.area.parent.name,
                        }
                    },
                    'id': show.venue.id,
                    'name': show.venue.name,
                }
            }
            shows.append(show_details)
        movie_details['shows'] = show_details

        category_list = []
        for category in movie.categories.all():
            category_details = {
                'id':category.id,
                'name':category.name,
                'parent_id':category.parent.id,
            }
            category_list.append(category_details)
        movie_details['categories'] = category_list

        results.append(movie_details)
    return results

The data is returned by django rest framework 0.4.0

Upvotes: 0

Views: 94

Answers (1)

Stefano
Stefano

Reputation: 3215

import json
json_obj = json.load(json_string)

Upvotes: 1

Related Questions