-2

I'm trying to just parse this so that I only get the name and bot_id

I used json.loads and did things like

for item in response:
    print item['bot_id']

Right now I'm just most concerned with getting the bot_id

def view_bot_ids():
    response = json.loads(requests.get("https://api.groupme.com/v3/bots?token=CANTSHOWTHIS")._content)
    print response

This is the output I am getting:

{
    u'meta': {u'code': 200},
    u'response': [
        {u'group_id': u'49818165', u'name': u'Johnny Five',
         u'dm_notification': False, u'group_name': u'Travis Manion Presentation', 
         u'avatar_url': None, u'callback_url': None,
         u'bot_id': u'240b08e530d42f286f30a75379'
        },
        {u'group_id': u'48672722', u'name': u'Johnny Five', 
         u'dm_notification': False, u'group_name': u'DevOps Autodidact', 
         u'avatar_url': None, u'callback_url': None,
         u'bot_id': u'64395a02a9382796f7cd7616ef'
        }, 
        {u'group_id': u'48402248', u'name': u'suck ya mom', 
         u'dm_notification': False, u'group_name': u'Free Flicks', 
         u'avatar_url': None, u'callback_url': None,
         u'bot_id': u'42aacdb69615721d68c31d71c0'
        },
        {u'group_id': u'43195303', u'name': u'The goat', 
         u'dm_notification': False, u'group_name': u'2nd Floor Boiz', 
         u'avatar_url': None, u'callback_url': None,
         u'bot_id': u'd45a95b6bbb344639104fd6a3a'
        }
    ]
}

All I want from this, is all the bot_ids and name.

All I want it to output is the array of bot ids or array of names.

Ray Toal
  • 86,166
  • 18
  • 182
  • 232
xXSlothAIXx
  • 5
  • 1
  • 3

6 Answers6

0
bot_ids, names = [], []
for x in response:
    bot_ids.append(x['bot_id'])
    names.append(x['name'])
print bot_ids
print names
  • 1
    Please don't only post code as an answer, make sure to include an explanation what your code does and how it solves the problem of the question. That makes your answer more valuable and more likely to attract upvotes. – Mark Rotteveel May 25 '19 at 06:28
0

Taking your output as an example:

FULL_RESPONSE = {
    u'meta': {u'code': 200},
    u'response': [
        {u'group_id': u'49818165', u'name': u'Johnny Five',
         u'dm_notification': False, u'group_name': u'Travis Manion Presentation',
         u'avatar_url': None, u'callback_url': None,
         u'bot_id': u'240b08e530d42f286f30a75379'
        },
        {u'group_id': u'48672722', u'name': u'Johnny Five',
         u'dm_notification': False, u'group_name': u'DevOps Autodidact',
         u'avatar_url': None, u'callback_url': None,
         u'bot_id': u'64395a02a9382796f7cd7616ef'
        },
        {u'group_id': u'48402248', u'name': u'suck ya mom',
         u'dm_notification': False, u'group_name': u'Free Flicks',
         u'avatar_url': None, u'callback_url': None,
         u'bot_id': u'42aacdb69615721d68c31d71c0'
        },
        {u'group_id': u'43195303', u'name': u'The goat',
         u'dm_notification': False, u'group_name': u'2nd Floor Boiz',
         u'avatar_url': None, u'callback_url': None,
         u'bot_id': u'd45a95b6bbb344639104fd6a3a'
        }
    ]
}

response = FULL_RESPONSE['response']

bot_ids = list()
names = list()
for item in response:
    bot_ids.append(item['bot_id'])
    names.append(item['name'])

print(bot_ids)
print(names)
Dino
  • 446
  • 4
  • 12
0

Another approach is to use list comprehension and then call np.array() on the nested list

numpy_2d_arrays  = np.array([[s['bot_id'], s['name']] for s in js['response']])

print(numpy_2d_arrays)
[['240b08e530d42f286f30a75379' 'Johnny Five']
 ['64395a02a9382796f7cd7616ef' 'Johnny Five']
 ['42aacdb69615721d68c31d71c0' 'xxxxxx']
 ['d45a95b6bbb344639104fd6a3a' 'The goat']]
edesz
  • 11,756
  • 22
  • 75
  • 123
0
def view_bot_ids(getField):
    ourData = []
    response = requests.get("https://api.groupme.com/v3/bots?token=CANTSHOWTHIS")
    if response.status_code == 200:
        for item in response.json():
            ourData.append(response[item].get(getField))
    return ourData


print(view_bot_ids('name'))
print(view_bot_ids('bot_id'))
VeNoMouS
  • 314
  • 2
  • 5
0

You could also group all dictionaries by key with a list of values:

groups = {}

for d in response['response']:
    for key, value in d.items():
        groups.setdefault(key, []).append(value)

print(groups['name'])
print(groups['bot_id'])
HTF
  • 6,632
  • 6
  • 30
  • 49
0

THIS WORKED: I just loaded the response.text and then loaded it with JSON, I realized that the response element worked so I just iterated through the response array and called bot_id and name. Then I just assigned variables to the item elements I wanted and added them to an array.

myRequest = requests.get("https://api.groupme.com/v3/bots?token=token1234")

json_str = myRequest.text

data = json.loads(json_str)

bot_ids = []
names = []
for item in data['response']:
    name = item['name']
    bot_id =  item['bot_id']
    names.append(name)
    bot_ids.append(bot_id)

print bot_ids print names

xXSlothAIXx
  • 5
  • 1
  • 3