1

I have a URL which gives a JSON result. I can parse the json and print the data from the JSON. But my main intention is to group each content and print them into an Excel sheet.

The code I wrote is:

import json
import urllib2
url = urllib2.urlopen("urlquery")
data = json.load(url)
print data

The data is printed in the dict type. Here each content has at least 10 parameters like ID, Name, Rating, Genre, Poster, etc.

How can I group each content with all the parameters? I am stuck here but from the group I know how to print into an Excel sheet.

Sample JSON response is below:

{
    facets: { },
    contents: [
       {
           id: "groupid://7110070",
           type: "group",
           adult: false,
           source: "group",
           instanceId: "groupid://7110070",
           parentalRating: {
           scheme: "custom",
           rating: 0
       },

I can't show the entire file due to security concerns.

Chris Martin
  • 30,334
  • 10
  • 78
  • 137
Roshan r
  • 522
  • 2
  • 11
  • 30
  • You need to show your desired output. As it is, this question is very unclear. – Daniel Roseman Sep 10 '15 at 09:00
  • @Chris.. Due to security concerns.. i didnt publish the whole file. There are 106 contents as per the JSON file. I need to group each content with all the parameters and should be able to print using a for loop for each content. – Roshan r Sep 10 '15 at 09:07
  • I presume that was a reply to me, not Chris. I'm not asking for the whole file, I just want an actual sample of what the output would look like. – Daniel Roseman Sep 10 '15 at 09:08
  • @Daniel.. very sorry that i misunderstood between you and Chris. My desired output is as follows from the previous example file. Content1.id = 7110070 Content1.type = group Content1.adult = false etc Content2.id = next value from json like wise to all 106 contents – Roshan r Sep 10 '15 at 09:10
  • I'm sorry, I don't understand at all. How is that different from what you have already? `content[0].id == 7110070`, etc. – Daniel Roseman Sep 10 '15 at 09:13
  • @Roshan, Add an example output to the question – amirouche Sep 10 '15 at 09:16
  • Dan.the previous file there are 106 contents. Each content will be having Id, type, adult etc. I need to group them just like an array or a dictionary. If i use a for loop inside each content, i should be able to get each parameter for each content. Each content will be having a poster URL also. So my main idea is to fetch the poster URL first, get that on a web page followed by all the other parameters. Then fetch the next poster and print the other parameters. Like wise, i need to do for all. – Roshan r Sep 10 '15 at 09:19
  • @amirouche.. Frankly speaking, i don't have an example output. Each content have a poster URL and many other parameters. So what i have to do is to group each content, fetch the poster from the URL and print on a web page followed by parameters for that content. Like wise, i need to do for all the remaining contents. I just asked the first part to get the grouping done and then i think i can carry out the printing on the web page stuff. – Roshan r Sep 10 '15 at 09:21
  • @Dan.. Actually those values are in the JSON. I am looking to extract those values from the JSON and keep them in an array or a dictionary. – Roshan r Sep 10 '15 at 09:55
  • But that can be done just with `result['contents']`. – Daniel Roseman Sep 10 '15 at 09:58
  • Dan.. I didn't get you here. – Roshan r Sep 10 '15 at 10:00

1 Answers1

10

You can use itertools.groupby to group contents using a given key. Have look at How do I use Python's itertools.groupby()? For more information.

In the following I group contents using the category key, which can be anything you want from the dictionary you have as input:

from itertools import groupby


contents = [
    dict(adult=True, id=111, name="Bob"),
    dict(adult=False, id=332, name="Chris"),
    dict(adult=True, id=113, name="John"),
    dict(adult=False, id=224, name="Amir"),
    dict(adult=True, id=115, name="Yann"),
    dict(adult=False, id=336, name="Lee"),
    dict(adult=False, id=227, name="Nadia"),
    dict(adult=False, id=228, name="Lucy")
]

# XXX: make sure to sort the content list
# with the key you want to group by
contents.sort(key=lambda content: content['adult'])

# then use groupby with the same key
groups = groupby(contents, lambda content: content['adult'])

for adult, group in groups:
    print 'adult', adult
    for content in group:
        print '\t', content

Here is the output:

adult False
    {'id': 332, 'name': 'Chris', 'adult': False}
    {'id': 224, 'name': 'Amir', 'adult': False}
    {'id': 336, 'name': 'Lee', 'adult': False}
    {'id': 227, 'name': 'Nadia', 'adult': False}
    {'id': 228, 'name': 'Lucy', 'adult': False}
adult True
    {'id': 111, 'name': 'Bob', 'adult': True}
    {'id': 113, 'name': 'John', 'adult': True}
    {'id': 115, 'name': 'Yann', 'adult': True}
Community
  • 1
  • 1
amirouche
  • 7,682
  • 6
  • 40
  • 94
  • amirouche.. I have a question here. Since the 'id' is from json file, how can i allocate the id values from there? From your example, i can allocate each category to be Name, type etc but how can i allocate the id ie values from the json? ] – Roshan r Sep 10 '15 at 09:49
  • what do you mean by «allocate» ? – amirouche Sep 10 '15 at 10:05
  • In the provided example, I only print the `id` but you have full access to the grouped dictionaries, so you can then use it to render the all the information found in the JSON contents dictionaries. – amirouche Sep 10 '15 at 10:06
  • For eg: 1st content have Name as Bob, Id as 1, Adult as false. 2nd content have Name as Chris , Id as 2, Adult as True. Now with the example you provided, i can set the category to be Name, id and Adult but how will assign the values corresponding to each category to ID? – Roshan r Sep 10 '15 at 10:08
  • What you have given are the static id's, right? The data for me will be changed dynamically. So i should parse the json and assign the values. Then print it. Hope its clearer now. – Roshan r Sep 10 '15 at 10:10
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/89273/discussion-between-amirouche-and-roshan-r). – amirouche Sep 10 '15 at 11:55