Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count occurrences of item in JSON element

Tags:

python

json

I'm using Python to parse the UK police API. What I want is to analyse the JSON response I'm getting in order to calculate how many times a certain offence occurs. This is an example of a response from the API.

{
    category: "anti-social-behaviour",
    location_type: "Force",
    location: {
        latitude: "53.349920",
        street: {
            id: 583315,
            name: "On or near Evenwood Close"
        },
        longitude: "-2.657889"
    },
    context: "",
    outcome_status: null,
    persistent_id: "",
    id: 22687179,
    location_subtype: "",
   month: "2013-03"
},

Using this code

from json import load
from urllib2 import urlopen
import json

url = "http://data.police.uk/api/crimes-street/all-crime?lat=53.396246&lng=-2.646960&date=2013-03"
json_obj = urlopen(url)
player_json_list = load(json_obj)

for player in player_json_list:
    crimeCategories = json.dumps(player['category'], indent = 2, separators=(',', ': '))
    print crimeCategories

I get a response like this

"anti-social-behaviour"
"anti-social-behaviour"
"anti-social-behaviour"
"anti-social-behaviour"
"drugs"
"drugs"
"burglary"

If I changed my for loop to

for player in player_json_list:
    crimeCategories = json.dumps(player['category'], indent = 2, separators=(',', ': '))
    print crimeCategories.count("drugs")

I then get a response like

0
0
0
0
1
1
0

Searching forums for hours hasn't helped me! Any ideas?

like image 705
Alex Chesters Avatar asked Mar 02 '15 21:03

Alex Chesters


1 Answers

You can use a collections.Counter dict with requests which becomes a couple of concise lines of code:

import  requests
from collections import Counter

url = "http://data.police.uk/api/crimes-street/all-crime?lat=53.396246&lng=-2.646960&date=2013-03"
json_obj = requests.get(url).json()

c = Counter(player['category'] for player in json_obj)
print(c)

Output:

Counter({'anti-social-behaviour': 79, 'criminal-damage-arson': 12, 'other-crime': 11, 'violent-crime': 9, 'vehicle-crime': 7, 'other-theft': 6, 'burglary': 4, 'public-disorder-weapons': 3, 'shoplifting': 2, 'drugs': 2})

If you prefer having a normal dict then simply call dict on the Counter dict:

from pprint import pprint as pp
c = dict(c)
pp(c)
{'anti-social-behaviour': 79,
 'burglary': 4,
 'criminal-damage-arson': 12,
 'drugs': 2,
 'other-crime': 11,
 'other-theft': 6,
 'public-disorder-weapons': 3,
 'shoplifting': 2,
 'vehicle-crime': 7,
 'violent-crime': 9}

You then simply access by key c['drugs'] etc..

Or iterate over the items to print crime and count in the format you want:

for k, v in c.items():
    print("{} count:  {}".format(k, v)

Output:

drugs count:  2
shoplifting count:  2
other-theft count:  6
anti-social-behaviour count:  79
violent-crime count:  9
criminal-damage-arson count:  12
vehicle-crime count:  7
public-disorder-weapons count:  3
other-crime count:  11
burglary count:  4
like image 177
Padraic Cunningham Avatar answered Nov 13 '22 07:11

Padraic Cunningham