-5

I make a GET to a API

I got this back

{"status":200,"message":"Success","data":[{"email_address":"admin@nyunets.com","password":"admin","account_id":1000,"account_type":"admin","name_prefix":null,"first_name":null,"middle_names":null,"last_name":"Admin","name_suffix":null,"non_person_name":false,"dba":"","display_name":"Admin","address1":"111 Park Ave","address2":"Floor 4","address3":"Suite 4011","city":"New York","state":"NY","postal_code":"10022","nation_code":"USA","phone1":"212-555-1212","phone2":"","phone3":"","time_zone_offset_from_utc":-5,"customer_type":2,"last_updated_utc_in_secs":1446127072},{"email_address":"mhn@nyu.com","password":"nyu123","account_id":1002,"account_type":"customer","name_prefix":"","first_name":"MHN","middle_names":"","last_name":"User","name_suffix":"","non_person_name":false,"dba":"","display_name":"MHNUser","address1":"3101 Knox St","address2":"","address3":"","city":"Dallas","state":"TX","postal_code":"75205","nation_code":"USA","phone1":"8623875097","phone2":"","phone3":"","time_zone_offset_from_utc":-5,"customer_type":2,"last_updated_utc_in_secs":1461166172},{"email_address":"mhn1@nyu.com","password":"nyu123","account_id":1004,"account_type":"customer","name_prefix":"","first_name":"MHN1","middle_names":"","last_name":"User","name_suffix":"","non_person_name":false,"dba":"","display_name":"MHN1User","address1":"1010 Rosedale Shopping Center","address2":"","address3":"","city":"Roseville","state":"MN","postal_code":"55113","nation_code":"USA","phone1":"8279856982","phone2":"","phone3":"","time_zone_offset_from_utc":-5,"customer_type":2,"last_updated_utc_in_secs":1461166417},{"email_address":"location@nyu.com","password":"nyu123","account_id":1005,"account_type":"customer","name_prefix":"","first_name":"BB","middle_names":"","last_name":"HH","name_suffix":"","non_person_name":false,"dba":"","display_name":"BBHH","address1":"9906 Beverly Dr","address2":"9906 Beverly Dr","address3":"","city":"Beverly Hills","state":"CA","postal_code":"90210","nation_code":"90210","phone1":"3105559906","phone2":"","phone3":"","time_zone_offset_from_utc":-5,"customer_type":1,"last_updated_utc_in_secs":1461167224},{"email_address":"mbn1@nyu.com","password":"nyu123","account_id":1003,"account_type":"customer","name_prefix":"","first_name":"MBN1","middle_names":"","last_name":"User","name_suffix":"","non_person_name":false,"dba":"","display_name":"MBN1User","address1":"3200 S Las Vegas Blvd","address2":"","address3":"","city":"Las Vegas","state":"NV","postal_code":"89109","nation_code":"USA","phone1":"9273597497","phone2":"","phone3":"","time_zone_offset_from_utc":-5,"customer_type":1,"last_updated_utc_in_secs":1461593233},{"email_address":"mbn@nyu.com","password":"nyu123","account_id":1001,"account_type":"customer","name_prefix":"","first_name":"MBN","middle_names":"","last_name":"User","name_suffix":"","non_person_name":false,"dba":"","display_name":"MBNUser","address1":"300 Concord Road","address2":"","address3":"","city":"Billerica","state":"MA","postal_code":"01821","nation_code":"USA","phone1":"8127085695","phone2":"","phone3":"","time_zone_offset_from_utc":-5,"customer_type":1,"last_updated_utc_in_secs":1461784499},{"email_address":"usermbn@nyu.com","password":"nyu123","account_id":1006,"account_type":"customer","name_prefix":"","first_name":"User","middle_names":"","last_name":"MBN","name_suffix":"","non_person_name":false,"dba":"","display_name":"UserMBN","address1":"75 Saint Alphonsus Street","address2":"","address3":"","city":"Boston","state":"MA","postal_code":"01821","nation_code":"USA","phone1":"8127085695","phone2":"","phone3":"","time_zone_offset_from_utc":-5,"customer_type":1,"last_updated_utc_in_secs":1462285561},{"email_address":"emile.barnaby@example.com","password":"nyu123","account_id":2000,"account_type":"customer","name_prefix":"","first_name":"emile","middle_names":"","last_name":"barnaby","name_suffix":"","non_person_name":false,"dba":"","display_name":"emilebarnaby","address1":"300 Concord Rd","address2":"","address3":"","city":"8239grandmaraisave","state":"manitoba","postal_code":"56798","nation_code":"USA","phone1":"414-140-1435","phone2":"414-140-1435","phone3":"414-140-1435","time_zone_offset_from_utc":-5,"customer_type":1,"last_updated_utc_in_secs":1462211572}]}
    

I have

import requests
import json

url = "http://api/users"
accounts = requests.get(url).json()
data = json.loads(accounts)

object_with_max_account_id = max(accounts['data'], key=lambda x: x['account_id'])
print(object_with_max_account_id['account_id'])

Goal

is to get the highest account id out of it.

halfer
  • 19,824
  • 17
  • 99
  • 186
code-8
  • 54,650
  • 106
  • 352
  • 604
  • What is this? How is your data stored? Are you using something built-in or a lib like numpy? – Two-Bit Alchemist May 17 '16 at 15:03
  • It's a sample response from an API call. – code-8 May 17 '16 at 15:05
  • 1
    Next time, work a bit harder and search around a bit before asking on SO, and include an [MVCE](http://stackoverflow.com/help/mcve). It's very frustrating to spend time answering a question only to have that question change so much that later readers find the answer doesn't address the later question (maybe to the point where it has to be deleted) when it *did* address the original. [Here are some tips on how to ask a good question](http://stackoverflow.com/help/how-to-ask). – WBT May 17 '16 at 15:50

2 Answers2

2

Usually we like to see what OPs try themselves, this is pretty much straightforward.

import requests

url = "http://api/users"
accounts = requests.get(url).json()

object_with_max_account_id = max(accounts['data'], key=lambda x: x['account_id'])
print(object_with_max_account_id['account_id'])
>>  2000
DeepSpace
  • 78,697
  • 11
  • 109
  • 154
1

Edit: Apparently, you first need to parse your input as JSON. Check out simplejson.

import simplejson as json
data_obj = json.loads(data)

The s in loads means load from string.
Then, if you want to be looping through, how about something like:

maxID= -1
for account in data_obj:
    if(account[account_id])>maxID:
        maxID= account[account_id]
print "Max ID is %d" % maxID
Community
  • 1
  • 1
WBT
  • 2,249
  • 3
  • 28
  • 40
  • @ihue Updated. Does that help? – WBT May 17 '16 at 15:20
  • I got this `TypeError: expected string or buffer` – code-8 May 17 '16 at 15:27
  • What is the result of `type(accounts)`? – WBT May 17 '16 at 15:34
  • `print type(accounts)` --> `` – code-8 May 17 '16 at 15:35
  • Look like I already got my answer. But +1 for trying to help. :) – code-8 May 17 '16 at 15:37
  • After you made the changes such that accounts is now a dictionary, did you try the second bit of code again as-is? You've been very rapidly changing what you are requesting here and establishing as preconditions. – WBT May 17 '16 at 15:37
  • So then are you going to stick with 'looping through' as originally requested, or are you going with the max() function as suggested in the other answer? – WBT May 17 '16 at 15:38
  • Your looping through is not working, and require me to install more things. which the other answer is not. – code-8 May 17 '16 at 15:40
  • It doesn't need anything new to be installed once you so significantly changed the question to have the input data already parsed as JSON. `simplejson` reportedly has better performance than the default `json` so it's an option for you, if you care about that. – WBT May 17 '16 at 15:42