Case Law Exercises

Case Law Exercises#

Exercise: Count Opinions per Author #

In the JSON case law data, each opinion has an author field. Complete the code below step-by-step to count the number of opinions per author.

You will need to browse the data.

a)#

Fetch the data results into cases as before. Then loop over the cases and print each case. Each case is a large dictionary, so expect a lot of text.

b)#

Now, change the content of the for loop. Instead of just printing the case, get the list of opinions. You will need to find the path to follow. For now, print the list of opinions.

c)#

Instead of printing the opinions, loop over the list of opinions and find and print the author.

d)#

Now we need to check if the author already exists in our lookup table/dictionary author2count. Use an if statement to start the count at one, if the author isn’t registered in the table.

e)#

Otherwise, if the author is registered in the dictionary, increase the count by one.

f)#

With the counting completed, all that remains is to show the results. Loop over author2count and print the result.

import requests
import json

URL = "https://api.case.law/v1/cases/?jurisdiction=ill&full_case=true&decision_date_min=2011-01-01&page_size=20"
data = requests.get(URL).json()

author2count = {} #example content {'JUSTICE CARTER': 1}
---------------------------------------------------------------------------
JSONDecodeError                           Traceback (most recent call last)
File /opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/requests/models.py:974, in Response.json(self, **kwargs)
    973 try:
--> 974     return complexjson.loads(self.text, **kwargs)
    975 except JSONDecodeError as e:
    976     # Catch JSON-related errors and raise as requests.JSONDecodeError
    977     # This aliases json.JSONDecodeError and simplejson.JSONDecodeError

File /opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/json/__init__.py:346, in loads(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    343 if (cls is None and object_hook is None and
    344         parse_int is None and parse_float is None and
    345         parse_constant is None and object_pairs_hook is None and not kw):
--> 346     return _default_decoder.decode(s)
    347 if cls is None:

File /opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/json/decoder.py:337, in JSONDecoder.decode(self, s, _w)
    333 """Return the Python representation of ``s`` (a ``str`` instance
    334 containing a JSON document).
    335 
    336 """
--> 337 obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    338 end = _w(s, end).end()

File /opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/json/decoder.py:355, in JSONDecoder.raw_decode(self, s, idx)
    354 except StopIteration as err:
--> 355     raise JSONDecodeError("Expecting value", s, err.value) from None
    356 return obj, end

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

During handling of the above exception, another exception occurred:

JSONDecodeError                           Traceback (most recent call last)
Cell In[1], line 5
      2 import json
      4 URL = "https://api.case.law/v1/cases/?jurisdiction=ill&full_case=true&decision_date_min=2011-01-01&page_size=20"
----> 5 data = requests.get(URL).json()
      7 author2count = {} #example content {'JUSTICE CARTER': 1}

File /opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/requests/models.py:978, in Response.json(self, **kwargs)
    974     return complexjson.loads(self.text, **kwargs)
    975 except JSONDecodeError as e:
    976     # Catch JSON-related errors and raise as requests.JSONDecodeError
    977     # This aliases json.JSONDecodeError and simplejson.JSONDecodeError
--> 978     raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)

JSONDecodeError: Expecting value: line 1 column 1 (char 0)