0

I'm trying to extract certain values from a JSON file. Here is my code.

ifile = open(ifile_name, 'r')

json_decode=json.load(ifile)
result = []
for item in json_decode:
    my_dict={}
    my_dict['Culture']['Movies']['2014']['Gravity']= item.get('Director')
    my_dict['Culture']['Movies']['2014']['Blue Jasmine'] = item.get('Director')
    print my_dict
    result.append(my_dict)
    back_jason=json.dumps(result, ofile)
    with open(ofile_name, "w+") as file :
        file.write(back_jason)

I'm trying to extract the name of a director who directed a movie in 2014. However, when I run the above code I am given the following error.

 my_dict['Culture']['Movies']['2014']['Gravity']= item.get('Director')
 AttributeError: 'unicode' object has no attribute 'get'

Can anyone explain why I am getting this error from my code?

Here is the JSON file

{
"Culture": {
    "Movies": {
        "2015": {
            "Birdman": {
                "Genre": "Comedy",
                "Director": "Alejandro Inarritu",
                "Oscars": 9,
                "Actors": [
                    "Michael Keaton",
                    "Enma Stone",
                    "Edward Norton",
                    "Naomi Watts"
                ]
            },
            "The Imitation Game": {
                "Genre": "Drama",
                "Director": "Morten Tyldum",
                "Oscars": 8,
                "Actors": [
                    "Benedict Cumberbatch",
                    "Keira Knightley",
                    "Matthew Goode"
                ]
            },
            "Magic in the Moonlight": {
                "Genre": "Comedy",
                "Director": "Woody Allen",
                "Oscars": 0,
                "Actors": [
                    "Enma Stone",
                    "Colin Firth",
                    "Marcia Harden"
                ]
            }
        },
        "2014": {
            "Gravity": {
                "Genre": "Drama",
                "Director": "Alfonso Cuaron",
                "Oscars": 10,
                "Actors": [
                    "Sandra Bullock",
                    "George Clooney",
                    "Ed Harris",
                    "Paul Sharma"
                ]
            },
            "Blue Jasmine": {
                "Genre": "Comedy",
                "Director": "Woody Allen",
                "Oscars": 1,
                "Actors": [
                    "Cate Blanchett",
                    "Sally Hawkins",
                    "Alec Baldwin"
                ]
            },
            "Blended": {
                "Genre": "Romance",
                "Director": "Frank Coraci",
                "Oscars": 0,
                "Actors": [
                    "Adam Sandler",
                    "Drew Barrymore",
                    "Jack Giarraputo"
                ]
            },
            "Ocho Apellidos Vascos": {
                "Genre": "Comedy",
                "Director": "Emilio Lazaro",
                "Oscars": 0,
                "Actors": [
                    "Dani Rovira",
                    "Clara Lago",
                    "Karra Elejalde",
                    "Carmen Machi"
                ]
            }
        }
    },
    "Books": {
        "2015": {
            "Go Set a Watchman": {
                "Genre": "Fiction",
                "Author": "Harper Lee",
                "Pages": 278
            },
            "The Girl on the Train": {
                "Genre": "Thriller",
                "Author": "Paula Hawkins",
                "Pages": 320
            }
        },
        "2014": {
            "El Barco de los Ninos": {
                "Genre": "Children",
                "Author": "Mario Llosa",
                "Pages": 96
            },
            "Sapiens": {
                "Genre": "History",
                "Author": "Yuval Harari",
                "Pages": 464
            }
        }
    }
}

}

Thank you.

4
  • Post what is in the file i.e. sample file content Commented Dec 4, 2015 at 17:36
  • @SIslam I have updated the question with the contents of the JSON file, sorry. Commented Dec 4, 2015 at 17:44
  • Would you clarify what you want to write in the second file (ofile_name)? - just director name? You are trying to construct a dictionary (my_dict) in bad way. Commented Dec 4, 2015 at 17:51
  • @SIslam in the ofile_name file I want a list of the directors. So in my case it will just be "Alfonson Cuaron" and "Woody Allen". (the second file is a .txt file) Commented Dec 4, 2015 at 17:58

1 Answer 1

3

Try just iterator as key to dictionary-

import json

ifile = open(r"D:\tst.txt", 'r')

json_decode=json.load(ifile)
result = []
for i in json_decode['Culture']['Movies']['2014']:
    data = json_decode['Culture']['Movies']['2014'][i]['Director']
    print data
    result.append(data)

with open(r"D:\tst1.txt", "w+") as file :
    for j in result:
        file.write(j+'\n')

Output file content-

Frank Coraci
Emilio Lazaro
Woody Allen
Alfonso Cuaron
Sign up to request clarification or add additional context in comments.

1 Comment

This worked perfectly, I have a much better understanding now. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.