helpmeplease
helpmeplease

Reputation: 1

How to input a JSON file an print a line

Write a program that inputs a JSON file (format just like example1.json) and prints out the value of the title field.

import json

# TODO: Read your json file here and return the contents
def read_json(filename):
    dt = {}
    # read the file and store the contents in the variable 'dt'
    with open(filename,"r") as fh:
        dt = json.load(fh)

    ###fh = open(filename, "r")
    ###dt = json.load(fh)

    return dt


# TODO: Pass the json file here and print the value of title field. Remove the `pass` statement
def print_title(dt):
    print filename["title"]


# TODO: Input a file from the user
filename = raw_input("Enter the JSON file: ")


# The function calls are already done for you
r = read_json(filename)
print_title(r)

Hi, I'm new with Python and I'm not sure what I am doing wrong. I keep getting the following message: enter image description here

Upvotes: 0

Views: 301

Answers (1)

omri_saadon
omri_saadon

Reputation: 10621

Your'e almost there, you just confused with the parameter name.

Change this:

def print_title(dt):
    print filename["title"]

To:

def print_title(dt):
    print dt["title"]

Upvotes: 2

Related Questions