Code Monkey home page Code Monkey logo

dsc-2-14-10-exploring-json-schemas-lab's Introduction

Exploring and Transforming JSON Schemas

Introduction

In this lesson, we'll formalize how to explore a JSON file whose structure and schema is unknown to you. This often happens in practice when you are handed a file or stumble upon one with little documentation.

Objectives

You will be able to:

  • Explore unknown JSON schemas
  • Access and manipulate data inside a JSON file
  • Convert JSON to alternative data formats

Loading the JSON file

Load the data from the file disease_data.json.

#Your code here 

Explore the first and second levels of the schema hierarchy.

#Your code here

Convert to a DataFrame

Create a DataFrame from the JSON file. Be sure to retrive the column names for the dataframe. (Search within the 'meta' key of the master dictionary.) The DataFrame should include all 42 columns.

#Your code here

Level-Up

Create a bar graph of states with the highest asthma rates for adults age 18+

Level-Up!

Create a function (or class) that returns an outline of the schema structure like this:

Rules:

  • Your outline should follow the numbering outline above (I, A, 1, a, i).
  • Your outline should be properly indented! (Four spaces or one tab per indentation level.)
  • Your function goes to at least a depth of 5 (Level-up: create a parameter so that the user can specify this)
  • If an entry is a dictionary, list its keys as the subheadings
  • After listing a key name (where applicable) include a space, a dash and the data type of the entry
  • If an entry is a dict or list put in parentheses how many items are in the entry
  • lists will not have key names for their entries (they're just indexed)
  • For subheadings of a list, state their datatypes.
  • If a dictionary or list is more then 5 items long, only show the first 5 (we want to limit our previews); make an arbitrary order choice for dictionaries. (Level-up: Parallel to above; allow user to specify number of items to preview for large subheading collections.)
# Your code here; you will probably want to define subfunctions.
def print_obj_outline(json_obj):
    return outline
outline = print_obj_outline(data)
print(outline) #Your function should produce the following output for this json object (and work for all json files!)
I. root - <class 'dict'> (2 items)
    A. meta <class 'dict'> (1 items)
        1. view <class 'dict'> (40 items)
            a. id <class 'str'> 
            b. name <class 'str'> 
            c. attribution <class 'str'> 
            d. attributionLink <class 'str'> 
            e. averageRating <class 'int'> 
    B. data <class 'list'> (60266 items)
        1. <class 'list'> (42 items)
            a. <class 'int'> 
            b. <class 'str'> 
            c. <class 'int'> 
            d. <class 'int'> 
            e. <class 'str'> 
        2. <class 'list'> (42 items)
            a. <class 'int'> 
            b. <class 'str'> 
            c. <class 'int'> 
            d. <class 'int'> 
            e. <class 'str'> 
        3. <class 'list'> (42 items)
            a. <class 'int'> 
            b. <class 'str'> 
            c. <class 'int'> 
            d. <class 'int'> 
            e. <class 'str'> 
        4. <class 'list'> (42 items)
            a. <class 'int'> 
            b. <class 'str'> 
            c. <class 'int'> 
            d. <class 'int'> 
            e. <class 'str'> 
        5. <class 'list'> (42 items)
            a. <class 'int'> 
            b. <class 'str'> 
            c. <class 'int'> 
            d. <class 'int'> 
            e. <class 'str'> 

Summary

Well done! In this lab you got some extended practice exploring the structure of JSON files and writing a recursive generalized function for outlining a JSON file's schema!

dsc-2-14-10-exploring-json-schemas-lab's People

Contributors

loredirick avatar mathymitchell avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

dsc-2-14-10-exploring-json-schemas-lab's Issues

Writing classes into empty modules

The files that the students are currently trying to import are blank so there is no class to pull in. We have been telling the students in the immersive to clone it locally and then use an ide toadd in the classes to the necessary files (ride.py, etc).

Ideally do we want students to open the files and add in the classes in the notebook? If so, they need direction on how to do this. A sample line of code or a reference to a previous lab if it is there. Right now students are just running
from ride import Ride
and getting the error that no module exists.

disease_data.json doesn't exist

Load the data from the file disease_data.json

FileNotFoundError Traceback (most recent call last)
in
2 import json
3
----> 4 f = open('disease_data.json', 'r')
5 data = json.load(f)

FileNotFoundError: [Errno 2] No such file or directory: 'disease_data.json'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.