Python json list index

list index error while converting json using python

Apparently one of your indices is out of bounds. Have you done any debugging? Also, show sample data.

Welcome to SO. Please take the time to read How to Ask and the links it contains. docs.python.org/3/library/exceptions.html#IndexError

Maybe [i+1] is a mistake but I have no way of knowing. You definatlty should have included a json example.

3 Answers 3

Since you are looking for longitude and latitude, I am guessing that the i+1 is wrong as suggested by @Hadus. Also, some of the lookups can be moved out of the print to make is cleaner:

LONGITUDE=0 LATTITUDE=1 while i< (len(dict_data)): coords = dict_data['features'][i]['geometry']['coordinates'] properties = dict_data['features'][i]['properties'] print( str(coords[LONGITUDE]) +"\t"+ str(coords[LATITUDE])+ "\t"+ str(properties['title']) +"\t"+ str(properties['place']) +"\t"+ str(properties['mag'])) i=i+1 

you comparing i with length of dict_data dictionary. But in print statement you use i to index dict_data['features'] field and (!) dict_data['features'][i]['geometry']['coordinates'] field. Also, you using i+1 index. I need more information (json example at least) to say for sure what's going wrong.

The coordinates are a list (of length 2) not a dictionary.

 print(str(dict_data['features'][i]['geometry']['coordinates'][0]) +"\t"+ str(dict_data['features'][i]['geometry']['coordinates'][1])+ "\t"+ str(dict_data['features'][i]['properties']['title']) +"\t"+ str(dict_data['features'][i]['properties']['place']) +"\t"+ str(dict_data['features'][i]['properties']['mag'])) 

@Andrew Gillis 's answer is better and I would recommend it unless you want ugly code what is never good.

Источник

How to extract first item in this json list

I have this json that im trying to extract the first element/list in this list of lists in python. How do you extract the first item in ads_list? Are there any built in functions i can use to extract first item in a json file? I need something other than simply iterating through this like an array. P.S. i shortened the json data Here is the list.

< u'data':< u'ad_list':[ < u'data':< u'require_feedback_score':0, u'hidden_by_opening_hours':False, u'trusted_required':False, u'currency':u'EGP', u'require_identification':False, u'is_local_office':False, u'first_time_limit_btc':None, u'city':u'', u'location_string':u'Egypt', u'countrycode':u'EG', u'max_amount':u'20000', u'lon':0.0, u'sms_verification_required':False, u'require_trade_volume':0.0, u'online_provider':u'SPECIFIC_BANK', u'max_amount_available':u'20000', u'msg': u" \u2605\u2605\u2605\u2605\u2605 \u0645\u0631\u062d\u0628\u0627 \u2605\u2605\u2605\u2605\u2605\r\n\r\n\u0625\u0630\u0627 \u0643\u0646\u062a \u062a\u0631\u063a\u0628 \u0641\u064a \u0628\u064a\u0639 \u0627\u0648 \u0634\u0631\u0627\u0621 \u0627\u0644\u0628\u062a\u0643\u0648\u064a\u0646 \u062a\u0648\u0627\u0635\u0644 \u0645\u0639\u064a \u0648\u0633\u0623\u0642\u0648\u0645 \u0628\u062e\u062f\u0645\u062a\u0643\r\n\u0644\u0644\u062a\u0648\u0627\u0635\u0644: https: //tawk.to/hanyibrahim\r\n \u0627\u0644\u062e\u064a\u0627\u0631 \u0644\u0644\u062a\u062d\u0648\u064a\u0644: \u0627\u0644\u0628\u0646\u0643 \u0627\u0644\u062a\u062c\u0627\u0631\u064a \u0627\u0644\u062f\u0648\u0644\u064a \u0627\u0648\u0641\u0648\u062f\u0627\u0641\u0648\u0646 \u0643\u0627\u0634 \u0627\u0648 \u0627\u062a\u0635\u0627\u0644\u0627\u062a \u0641\u0644\u0648\u0633 \u0627\u0648 \u0627\u0648\u0631\u0627\u0646\u062c \u0645\u0648\u0646\u064a\r\n\r\n'' \u0634\u0643\u0631\u0627 ''\r\n\r\n\r\n \u2605\u2605\u2605\u2605\u2605 Hello \u2605\u2605\u2605\u2605\u2605\r\n\r\nIf you would like to trade Bitcoins please let me know and I will help you\r\nconnect: https: //tawk.to/hanyibrahim\r\nOption transfer:Bank CIB Or Vodafone Cash Or Etisalat Flous Or Orange Money\r\n'' Thank ''", u'volume_coefficient_btc':u'1.50', u'profile':< u'username':u'hanyibrahim11', u'feedback_score':100, u'trade_count':u'3000+', u'name':u'hanyibrahim11 (3000+; 100%)', u'last_online': u'2019-01-14T17:54:52+00:00 '>, u' bank_name':u'CIB_Vodafone Cash_Etisalat Flous_Orange Money', u'trade_type':u'ONLINE_BUY', u'ad_id':803036, u'temp_price':u'67079.44', u'payment_window_minutes':90, u'min_amount':u'50', u'limit_to_fiat_amounts':u'', u'require_trusted_by_advertiser':False, u'temp_price_usd':u'3738.54', u'lat':0.0, u'visible':True, u'created_at': u'2018-07-25T08:12:21+00:00 ', u' atm_model':None, u'is_low_risk':True >, u'actions': < u'public_view': u'https://localbitcoins.com/ad/803036' >>, < u'data':< u'require_feedback_score':0, u'hidden_by_opening_hours':False, u'trusted_required':False, u'currency':u'EGP', u'require_identification':False, u'is_local_office':False, u'first_time_limit_btc':None, u'city':u'', u'location_string':u'Egypt', u'countrycode':u'EG', u'max_amount':u'20000', u'lon':0.0, u'sms_verification_required':False, u'require_trade_volume':0.0, u'online_provider':u'CASH_DEPOSIT', u'max_amount_available':u'20000', u'msg':u'QNB, CIB deposite- Vodafone Cash - Etisalat Felous - Orange Money - Western Union - Money Gram \r\n- Please do not entiate a new trade request if you are not serious to finalize it.', u'volume_coefficient_btc':u'1.50', u'profile':< u'username':u'Haboush', u'feedback_score':99, u'trade_count':u'500+', u'name':u'Haboush (500+; 99%)', u'last_online': u'2019-01-14T16:48:52+00:00 '>, u' bank_name':u'QNB\u2714CIB\u2714Vodafone\u2714Orange\u2714Etisalat\u2714WU', u'trade_type':u'ONLINE_BUY', u'ad_id':719807, u'temp_price':u'66860.18', u'payment_window_minutes':270, u'min_amount':u'100', u'limit_to_fiat_amounts':u'', u'require_trusted_by_advertiser':False, u'temp_price_usd':u'3726.32', u'lat':0.0, u'visible':True, u'created_at': u'2018-03-24T19:29:08+00:00 ', u' atm_model':None, u'is_low_risk':True >, u'actions': < u'public_view': u'https://localbitcoins.com/ad/719807' >>, > ], u'ad_count':17 > > 

1. this isn't valid JSON, it looks like a python2 representation of what could be parsed JSON. 2. if you properly parsed the JSON there is no reason you shouldn't be able to just get the first element by index. What have you tried, what error did you receive or how did the result vary from what you expected?

Читайте также:  Simple Page

Источник

Parsing JSON with python- list indices must be integer not str

I am beginner at python and am trying to parse the following JSON. I am not able to find out how to get the artist name and title of the song.

< "status": < "msg": "Success", "code": 0, "version": "1.0" >, "metadata": < "music": [ < "external_ids": < "isrc": "USSM10603618", "upc": "888880170897" >, "play_offset_ms": 8920, "external_metadata": < "spotify": < "album": < "id": "0JLv6iVbeiy4Dh2eIw6FKI" >, "artists": [ < "id": "6vWDO969PvNqNYHIOW5v0m" >], "track": < "id": "3qSMg1lhn4jDwWlI9xCVyK" >>, "itunes": < "album": < "id": 464320979 >, "artists": [ < "id": 1419227 >], "track": < "id": 464321089 >>, "deezer": < "album": < "id": 72429 >, "artists": [ < "id": 145 >], "genres": [ < "id": 132 >], "track": < "id": 551232 >> >, "title": "Listen (From the Motion Picture \"Dreamgirls\")", "duration_ms": "217786", "album": < "name": "B'Day Deluxe Edition" >, "acrid": "4660601066a3153acf15eabe2868572b", "genres": [ < "name": "Pop" >], "artists": [ < "name": "Beyoncé" >] > ], "timestamp_utc": "2015-07-27 10:35:28" >, "result_type": 0 > 
json_r=json.loads(res) print(json_r) for i in json_r: song_name=json_r.metadata['music']['title'] print song_name artist=json_r['metadata']['music']['artists']['name'] s_t_id=json_r['metadata']['music']['external_metadata']['spotify']['track']['id'] s_a_id=json_r['metadata']['music']['external_metadata']['spotify']['artists']['id'] 

Источник

Оцените статью