user2843235
user2843235

Reputation: 117

Getting information from json and using it in a function

I'm a beginner for Python. The thing I'm having trouble is selecting the url from my array after searching for the song and using that url to be played in the webbrowser.open_new_tab(). The py file is:

import json
import Link_Class
import Music_Database
from pprint import pprint
m = Link_Class.MusicLink()
import webbrowser

search = raw_input("Find this Song: ")
results= m.searchSong(search)
pprint(results)

My json file is:

{"LinkCollection":

[{"title":"I Will Always Love You" , 
"artist":"Whitney Houston" ,
"link":"http://www.youtube.com/watch?v=3JWTaaS7LdU", 
"id":1},

{"title":"Killing Me Softly" , 
"artist":"Roberta Flack" ,
"link":"http://www.youtube.com/watch?v=LQ2t5e7stVM", 
"id":2}
]}

There are more, but for simplicity, I did not write all the data here.

Upvotes: 0

Views: 58

Answers (2)

evuez
evuez

Reputation: 3387

It can be done quite simply with json.load:

with open('your_file.json', 'r') as out:
    data = json.load(out)

# then fetch you data

for song in data['LinkCollection']:
    if song['title'] == search:
        webbrowser.open_new_tab(song['link'])
        break

Upvotes: 1

FogleBird
FogleBird

Reputation: 76772

Do you just want to open the first result?

link = results['LinkCollection'][0]['link']
webbrowser.open_new_tab(link)

Upvotes: 0

Related Questions