Lab 7 Semi-structured data
Purpose: This lab aims to provide you a practical example of the usefulness of nested recursive data structures.
1 Introduction (5 minutes)
(require "lab7-teachpack.rkt")
2 JSON (95 minutes)
Goals: Practice working with mutually recursive data. Practice working with tree-structured data.
; A JSON is one of: ; - String ; - Number ; - Boolean ; - 'null ; - JSONArray ; - JSONObject ; A JSONArray is a [List-of JSON] ; A JSONObject is a [List-of JSONPair] ; A JSONPair is a (list Symbol JSON)
Sample Problem JSON Examples: Provide some examples of a JSON, a JSONArray, a JSONObject, and a JSONPair. You can give some simple examples, but also provide some more nested examples to show how the data definitions are intertwined
(define JSON1 "hello") (define JSON2 'null) (define JSONARRAY1 (list JSON1 JSON2)) (define JSONARRAY2 (list (list JSON1 JSON2) JSON1 (list (list JSON2)))) (define JSONOBJECT1 (list (list 'a 1) (list 'b 2) (list 'c 3))) (define JSONOBJECT2 (list (list 'numbers (list 1 2 3 4)) (list 'strings (list "a" "b" "c")) (list 'nesting (list (list 'inner1 (list (list 1 2) (list "a" "b"))) (list 'inner2 (list (list (list 'hi "bye")) "hello")))))) Hint 1: Can you use quoted lists to write the more complicated examples above more easily?
Hint 2: If you add (require racket) to your code, you will get access to the pretty-print function, which is very handy for printing out large chunks of list structure in a nice way. Try, for example, (pretty-print JSONOBJECT2).
Sample Problem JSON Templates: Write a template for JSON. Note that JSONObjects and JSONArrays both contain JSON, so you must write a template that is mutually recursive. Since the template is just a comment, feel free to assume you’ve already completed the json-pair?, json-object?, and json-array? functions.
; json-temp : JSON -> ??? (define (json-temp ajson) (... (cond [(string? ajson) ...] [(number? ajson) ...] [(boolean? ajson) ...] [(symbol? ajson) ...] [(json-array? ajson) ...(json-array-temp ajson) ...] [(json-object? ajson) ... (json-object-temp ajson) ...]) ...)) ; json-array-temp : JSONArray -> ??? (define (json-array-temp ja) (... (cond [(empty? ja) ...] [(cons? ja) ... (json-temp (first ja)) ... ... (json-array-temp (rest ja)) ...]) ...)) ; json-object-temp : JSONObject -> ??? (define (json-object-temp jo) (... (cond [(empty? jo) ...] [(cons? jo) ... (json-pair-temp (first jo)) ... ... (json-object-temp (rest jo)) ...]) ...)) ; json-pair-temp : JSONPair -> ??? (define (json-pair-temp jp) (... (first jp) ... (json-temp (second jp)) ...))
Exercise 1 Predicates: Write the functions json-pair?, json-object?, json-array?, and json? that when given any input returns a Boolean. These functions should only return true when the given input is valid. Be sure to check the whole value (i.e. all values in an array). You will notice that these functions are mutually recursive, just like the templates we wrote!
Exercise 2 json=?: Design the function json=? that will determine if two JSONs are equal. For the sake of simplicity, you can assume order of JSONPairs within a JSONObject matters here.
You can get weather data turned into JSON using one of the APIs we have made available.
(get-url "http://tinyurl.com/hmsbkb6")
(get-url "http://tinyurl.com/zbqnrhp")
Sample Problem Weather Data: Define weather-data as the result of calling string->json on one of the get-url calls above. This should provide you with real actual JSON to work with which matches our data definition.
(define weather-data (get-url "http://tinyurl.com/hmsbkb6"))
Exercise 3 find-value: Design a function find-value that takes a JSON and a Symbol and returns the first JSON value from a JSONPair where the name is equal to the given Symbol. Be sure to test your function thoroughly!
Exercise 4 max-temp: Design a function max-temp which, given a JSON, returns the maximum temperature from the data as a Number (look at the data you got back from the weather service to see what label they use for the maximum temperature).
Exercise 5 average-humidity: Design a function average-humidity which, given a JSON, returns the humidity from the data as a Number.
Exercise 6 current-weather: Design a function current-weather which given a JSON returns a [Listof String] of all the "description"s of the current weather from the data for the latest data point.
Exercise 7 weather-graph: Design the function weather-graph which given a JSON and a Symbol will return an Image. The image is a bar graph of each value of the given Symbol.
3 Before you go...
If you had trouble finishing any of the exercises in the lab or homework, or just feel like you’re struggling with any of the class material, please feel free to come to office hours and talk to a TA or tutor for additional assistance.