Python json.get() Examples
The following are 30 code examples for showing how to use json.get(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
You may check out the related API usage on the sidebar.
You may also want to check out all available functions/classes of the module
json
, or try the search function
.
Example 1
Project: dataiku-contrib Author: dataiku File: intercomapi.py License: Apache License 2.0 | 7 votes |
def list_object(object_name, token, parameters = {}): """ A generator that wraps make_api_call() to get all items of an object """ looping = True parameters.update({'page': None}) while looping: json = make_api_call(object_name, token, parameters) logging.info('Intercom plugin - %s: results = %i' % (object_name, len(json.get(object_name))) ) for r in json.get(object_name): yield r next = json.get('pages', {}).get('next') logging.info('Intercom plugin - %s: next = %s' % (object_name, next) ) if next is None: looping = False else: # next contains an url, let's extract the url params new_params = dict(urlparse.parse_qs(urlparse.urlparse(next).query)) parameters.update(new_params)
Example 2
Project: dataiku-contrib Author: dataiku File: pipedriveapi.py License: Apache License 2.0 | 6 votes |
def make_api_call_all_pages(conf, action, params = {}): """ A wrapper of make_api_call() to get all pages on a GET request """ start = 0 results = [] looping = True params.update({'limit':conf['PAGINATION']}) while looping: params.update({'start':start}) json = make_api_call(conf, action, params) for r in json.get('data'): results.append(r) is_more = json.get('additional_data').get('pagination').get('more_items_in_collection') if is_more: start = json.get('additional_data').get('pagination').get('next_start') else: looping = False return results
Example 3
Project: dataiku-contrib Author: dataiku File: intercomapi.py License: Apache License 2.0 | 6 votes |
def make_api_call(action, token, parameters = {}, method = 'get', data = {}): """ Makes an API call to Intercom """ headers = { 'Content-type': 'application/json', 'Accept': 'application/json', 'Accept-Encoding': 'gzip', 'Authorization': 'Bearer %s' % token } if method == 'get': r = s.request(method, 'https://api.intercom.io/'+action, headers=headers, params=parameters, timeout=30) else: raise ValueError('Unimplemented method.') logging.info('Intercom plugin - API %s call: %s' % (method, r.url) ) if r.status_code < 300: return r.json() else: raise Exception('API error when calling %s (code %i): %s' % (r.url, r.status_code, r.content))
Example 4
Project: dataiku-contrib Author: dataiku File: intercomapi.py License: Apache License 2.0 | 6 votes |
def scroll_object(object_name, token, parameters = {}): """ A generator that wraps make_api_call() to get all items of an object using the scpecial scroll endpoint """ page = None looping = True while looping: parameters.update({'scroll_param':page}) json = make_api_call(object_name+'/scroll', token, parameters) logging.info('Intercom plugin - %s: scroll_param = %s' % (object_name, json.get("scroll_param")) ) logging.info('Intercom plugin - %s: results = %i' % (object_name, len(json.get(object_name))) ) for r in json.get(object_name): yield r page = json.get('scroll_param') if page is None or len(json.get(object_name)) == 0: looping = False
Example 5
Project: PyPardot Author: joshgeller File: client.py License: MIT License | 6 votes |
def get(self, object_name, path=None, params=None, retries=0): """ Makes a GET request to the API. Checks for invalid requests that raise PardotAPIErrors. If the API key is invalid, one re-authentication request is made, in case the key has simply expired. If no errors are raised, returns either the JSON response, or if no JSON was returned, returns the HTTP response status code. """ if params is None: params = {} params.update({'user_key': self.user_key, 'api_key': self.api_key, 'format': 'json'}) try: self._check_auth(object_name=object_name) request = requests.get(self._full_path(object_name, path), params=params) response = self._check_response(request) return response except PardotAPIError as err: if err.message == 'Invalid API key or user key': response = self._handle_expired_api_key(err, retries, 'get', object_name, path, params) return response else: raise err
Example 6
Project: rtkbase Author: Stefal File: server.py License: GNU Affero General Public License v3.0 | 6 votes |
def login_page(): if current_user.is_authenticated: return redirect(url_for('status_page')) loginform = LoginForm() if loginform.validate_on_submit(): user = User('admin') password = loginform.password.data if not user.check_password(password): return abort(401) login_user(user, remember=loginform.remember_me.data) next_page = request.args.get('next') if not next_page or url_parse(next_page).netloc != '': next_page = url_for('status_page') return redirect(next_page) return render_template('login.html', title='Sign In', form=loginform)
Example 7
Project: retdec-regression-tests-framework Author: avast File: config_parser.py License: MIT License | 6 votes |
def vtables(self): """Virtual tables (list of :class:`Vtable`). The returned list can be indexed by either positions (0, 1, ...) or vtable names. Example: .. code-block:: python module.vtables[0] # Returns the first vtable. module.vtables['vt1'] # Returns the vtable named 'vt1'. """ vtables = NamedObjectList() for vt in self.json.get('vtables', []): vtables.append(Vtable(vt)) return vtables
Example 8
Project: retdec-regression-tests-framework Author: avast File: config_parser.py License: MIT License | 6 votes |
def classes(self): """C++ classes (list of :class:`Class`). The returned list can be indexed by either positions (0, 1, ...) or classes' names. Example: .. code-block:: python module.classes[0] # Returns the first class. module.classes['class1'] # Returns the class named 'class1'. """ classes = NamedObjectList() for c in self.json.get('classes', []): classes.append(Class(c)) return classes
Example 9
Project: histogrammar-python Author: histogrammar File: sum.py License: Apache License 2.0 | 6 votes |
def fromJsonFragment(json, nameFromParent): if isinstance(json, dict) and hasKeys(json.keys(), ["entries", "sum"], ["name"]): if json["entries"] in ("nan", "inf", "-inf") or isinstance(json["entries"], numbers.Real): entries = float(json["entries"]) else: raise JsonFormatException(json["entries"], "Sum.entries") if isinstance(json.get("name", None), basestring): name = json["name"] elif json.get("name", None) is None: name = None else: raise JsonFormatException(json["name"], "Sum.name") if json["sum"] in ("nan", "inf", "-inf") or isinstance(json["sum"], numbers.Real): sum = float(json["sum"]) else: raise JsonFormatException(json["sum"], "Sum.sum") out = Sum.ed(entries, sum) out.quantity.name = nameFromParent if name is None else name return out.specialize() else: raise JsonFormatException(json, "Sum")
Example 10
Project: histogrammar-python Author: histogrammar File: minmax.py License: Apache License 2.0 | 6 votes |
def fromJsonFragment(json, nameFromParent): if isinstance(json, dict) and hasKeys(json.keys(), ["entries", "min"], ["name"]): if json["entries"] in ("nan", "inf", "-inf") or isinstance(json["entries"], numbers.Real): entries = float(json["entries"]) else: raise JsonFormatException(json["entries"], "Minimize.entries") if isinstance(json.get("name", None), basestring): name = json["name"] elif json.get("name", None) is None: name = None else: raise JsonFormatException(json["name"], "Minimize.name") if json["min"] in ("nan", "inf", "-inf") or isinstance(json["min"], numbers.Real): min = float(json["min"]) else: raise JsonFormatException(json["min"], "Minimize.min") out = Minimize.ed(entries, min) out.quantity.name = nameFromParent if name is None else name return out.specialize() else: raise JsonFormatException(json, "Minimize")
Example 11
Project: histogrammar-python Author: histogrammar File: average.py License: Apache License 2.0 | 6 votes |
def fromJsonFragment(json, nameFromParent): if isinstance(json, dict) and hasKeys(json.keys(), ["entries", "mean"], ["name"]): if json["entries"] in ("nan", "inf", "-inf") or isinstance(json["entries"], numbers.Real): entries = float(json["entries"]) else: raise JsonFormatException(json["entries"], "Average.entries") if isinstance(json.get("name", None), basestring): name = json["name"] elif json.get("name", None) is None: name = None else: raise JsonFormatException(json["name"], "Average.name") if json["mean"] in ("nan", "inf", "-inf") or isinstance(json["mean"], numbers.Real): mean = float(json["mean"]) else: raise JsonFormatException(json["mean"], "Average.mean") out = Average.ed(entries, mean) out.quantity.name = nameFromParent if name is None else name return out.specialize() else: raise JsonFormatException(json, "Average")
Example 12
Project: PyPardot4 Author: mneedham91 File: client.py License: MIT License | 6 votes |
def get(self, object_name, path=None, params=None, retries=0): """ Makes a GET request to the API. Checks for invalid requests that raise PardotAPIErrors. If the API key is invalid, one re-authentication request is made, in case the key has simply expired. If no errors are raised, returns either the JSON response, or if no JSON was returned, returns the HTTP response status code. """ if params is None: params = {} params.update({'format': 'json'}) headers = self._build_auth_header() try: self._check_auth(object_name=object_name) request = requests.get(self._full_path(object_name, self.version, path), params=params, headers=headers) response = self._check_response(request) return response except PardotAPIError as err: if err.message == 'Invalid API key or user key': response = self._handle_expired_api_key(err, retries, 'get', object_name, path, params) return response else: raise err
Example 13
Project: PyPardot4 Author: mneedham91 File: client.py License: MIT License | 6 votes |
def authenticate(self): """ Authenticates the user and sets the API key if successful. Returns True if authentication is successful, False if authentication fails. """ try: auth = self.post('login', params={'email': self.email, 'password': self.password}) if type(auth) is int: # sometimes the self.post method will return a status code instead of JSON response on failures return False self.api_key = auth.get('api_key', None) if self.api_key is not None: return True return False except PardotAPIError: return False
Example 14
Project: tbapy Author: frc1418 File: main.py License: MIT License | 6 votes |
def _get(self, url): """ Helper method: GET data from given URL on TBA's API. :param url: URL string to get data from. :return: Requested data in JSON format. """ extra_headers = {} if self._if_modified_since is not None: extra_headers['If-Modified-Since'] = self._if_modified_since response = self.session.get(self.READ_URL_PRE + url, headers=extra_headers) last_modified = response.headers.get('Last-Modified') if last_modified is not None: if response.status_code == 304: raise NotModifiedException(response.headers['Last-Modified']) if self._last_modified: self._last_modified = LastModifiedDate(last_modified) raw = response.json() self._detect_errors(raw) return raw
Example 15
Project: tbapy Author: frc1418 File: main.py License: MIT License | 6 votes |
def team_events(self, team, year=None, simple=False, keys=False): """ Get team events a team has participated in. :param team: Team to get events for. :param year: Year to get events from. :param simple: Get only vital data. :param keys: Get just the keys of the events. Set to True if you only need the keys of each event and not their full data. :return: List of strings or Teams """ if year: if keys: return self._get('team/%s/events/%s/keys' % (self.team_key(team), year)) else: return [Event(raw) for raw in self._get('team/%s/events/%s%s' % (self.team_key(team), year, '/simple' if simple else ''))] else: if keys: return self._get('team/%s/events/keys' % self.team_key(team)) else: return [Event(raw) for raw in self._get('team/%s/events%s' % (self.team_key(team), '/simple' if simple else ''))]
Example 16
Project: tbapy Author: frc1418 File: main.py License: MIT License | 6 votes |
def team_matches(self, team, event=None, year=None, simple=False, keys=False): """ Get list of matches team has participated in. :param team: Team to get matches of. :param year: Year to get matches from. :param event: Event to get matches from. :param simple: Get only vital data. :param keys: Only get match keys rather than their full data. :return: List of string keys or Match objects. """ if event: if keys: return self._get('team/%s/event/%s/matches/keys' % (self.team_key(team), event)) else: return [Match(raw) for raw in self._get('team/%s/event/%s/matches%s' % (self.team_key(team), event, '/simple' if simple else ''))] elif year: if keys: return self._get('team/%s/matches/%s/keys' % (self.team_key(team), year)) else: return [Match(raw) for raw in self._get('team/%s/matches/%s%s' % (self.team_key(team), year, '/simple' if simple else ''))]
Example 17
Project: dataiku-contrib Author: dataiku File: pipedriveapi.py License: Apache License 2.0 | 5 votes |
def make_api_call(conf, action, params = {}, method = 'get', data = {}): """ Make an API call to Pipedrive. The method can be 'get' or 'post'. """ parameters = { 'api_token': conf['API_KEY'], 'api_output': 'json' } parameters.update(params) headers = { 'Content-type': 'application/json' } if method == 'get': r = requests.request(method, conf['API_BASE_URL']+action, params=parameters) elif method == 'post': r = requests.request(method, conf['API_BASE_URL']+action, data=data, params=parameters) else: raise ValueError('Method should be get or post.') print 'API call: ' + r.url if ((r.status_code == 200 and method == 'get') or (r.status_code == 201 and method == 'post')) and r.json().get('success') == True: return r.json() else: if 'error' in r.json(): raise IOError('API error ("%s") when calling: %s' % (r.json().get('error'), r.url) ) else: raise IOError('API error (unknown) when calling: %s' % r.url )
Example 18
Project: PyPardot Author: joshgeller File: client.py License: MIT License | 5 votes |
def _check_response(response): """ Checks the HTTP response to see if it contains JSON. If it does, checks the JSON for error codes and messages. Raises PardotAPIError if an error was found. If no error was found, returns the JSON. If JSON was not found, returns the response status code. """ if response.headers.get('content-type') == 'application/json': json = response.json() error = json.get('err') if error: raise PardotAPIError(json_response=json) return json else: return response.status_code
Example 19
Project: PyPardot Author: joshgeller File: client.py License: MIT License | 5 votes |
def authenticate(self): """ Authenticates the user and sets the API key if successful. Returns True if authentication is successful, False if authentication fails. """ try: auth = self.post('login', params={'email': self.email, 'password': self.password}) self.api_key = auth.get('api_key') if self.api_key is not None: return True return False except PardotAPIError: return False
Example 20
Project: rtkbase Author: Stefal File: server.py License: GNU Affero General Public License v3.0 | 5 votes |
def __init__(self, username): self.id=username self.password_hash = rtkbaseconfig.get("general", "web_password_hash")
Example 21
Project: rtkbase Author: Stefal File: server.py License: GNU Affero General Public License v3.0 | 5 votes |
def update_password(config_object): """ Check in settings.conf if web_password entry contains a value If yes, this function will generate a new hash for it and remove the web_password value :param config_object: a RTKBaseConfigManager instance """ new_password = config_object.get("general", "new_web_password") if new_password != "": config_object.update_setting("general", "web_password_hash", generate_password_hash(new_password)) config_object.update_setting("general", "new_web_password", "")
Example 22
Project: rtkbase Author: Stefal File: server.py License: GNU Affero General Public License v3.0 | 5 votes |
def check_update(source_url = None, current_release = None, prerelease=False, emit = True): """ Check if a RTKBase update exists :param source_url: the url where we will try to find an update. It uses the github api. :param current_release: The current RTKBase release :param prerelease: True/False Get prerelease or not :param emit: send the result to the web front end with socketio :return The new release version inside a dict (release version and url for this release) """ new_release = {} source_url = source_url if source_url is not None else "https://api.github.com/repos/stefal/rtkbase/releases" current_release = current_release if current_release is not None else rtkbaseconfig.get("general", "version").strip("v") current_release = current_release.replace("-beta", "").replace("-alpha", "").replace("-rc", "") try: response = requests.get(source_url) response = response.json() for release in response: if release.get("prerelease") & prerelease or release.get("prerelease") == False: latest_release = release.get("tag_name").strip("v").replace("-beta", "").replace("-alpha", "").replace("-rc", "") if latest_release > current_release and latest_release <= rtkbaseconfig.get("general", "checkpoint_version"): new_release = {"new_release" : release.get("tag_name"), "url" : release.get("assets")[0].get("browser_download_url"), "comment" : release.get("body")} break except Exception as e: print("Check update error: ", e) if emit: socketio.emit("new release", json.dumps(new_release), namespace="/test") print return new_release
Example 23
Project: rtkbase Author: Stefal File: server.py License: GNU Affero General Public License v3.0 | 5 votes |
def update_rtkbase(): """ Check if a RTKBase exists, download it and update rtkbase """ #Check if an update is available update_url = check_update(emit=False).get("url") if update_url is None: return import tarfile #Download update update_archive = "/var/tmp/rtkbase_update.tar.gz" try: response = requests.get(update_url) with open(update_archive, "wb") as f: f.write(response.content) except Exception as e: print("Error: Can't download update - ", e) #Get the "root" folder in the archive tar = tarfile.open(update_archive) for tarinfo in tar: if tarinfo.isdir(): primary_folder = tarinfo.name break #Extract archive tar.extractall("/var/tmp") #launch update script rtk.shutdownBase() rtkbase_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "../")) source_path = os.path.join("/var/tmp/", primary_folder) script_path = os.path.join(source_path, "rtkbase_update.sh") current_release = rtkbaseconfig.get("general", "version").strip("v") standard_user = rtkbaseconfig.get("general", "user") os.execl(script_path, "unused arg0", source_path, rtkbase_path, app.config["DOWNLOAD_FOLDER"].split("/")[-1], current_release, standard_user)
Example 24
Project: rtkbase Author: Stefal File: server.py License: GNU Affero General Public License v3.0 | 5 votes |
def inject_release(): """ Insert the RTKBase release number as a global variable for Flask/Jinja """ g.version = rtkbaseconfig.get("general", "version")
Example 25
Project: rtkbase Author: Stefal File: server.py License: GNU Affero General Public License v3.0 | 5 votes |
def deleteLog(json): rtk.logm.deleteLog(json.get("name")) # Sending the the new available logs getAvailableLogs() #### Download and convert log handlers ####
Example 26
Project: rtkbase Author: Stefal File: server.py License: GNU Affero General Public License v3.0 | 5 votes |
def processLog(json): log_name = json.get("name") print("Got signal to process a log, name = " + str(log_name)) print("Path to log == " + rtk.logm.log_path + "/" + str(log_name)) raw_log_path = rtk.logm.log_path + "/" + log_name rtk.processLogPackage(raw_log_path)
Example 27
Project: rtkbase Author: Stefal File: server.py License: GNU Affero General Public License v3.0 | 5 votes |
def cancelLogConversion(json): log_name = json.get("name") raw_log_path = rtk.logm.log_path + "/" + log_name rtk.cancelLogConversion(raw_log_path) #### RINEX versioning ####
Example 28
Project: rtkbase Author: Stefal File: server.py License: GNU Affero General Public License v3.0 | 5 votes |
def writeRINEXVersion(json): rinex_version = json.get("version") rtk.logm.setRINEXVersion(rinex_version) #### Device hardware functions ####
Example 29
Project: lightning-python Author: lightning-viz File: visualization.py License: MIT License | 5 votes |
def __init__(self, session=None, json=None, auth=None): self.session = session self.id = json.get('id') self.auth = auth if self.session.lgn.ipython_enabled: from ipykernel.comm import Comm self.comm = Comm('lightning', {'id': self.id}) self.comm_handlers = {} self.comm.on_msg(self._handle_comm_message)
Example 30
Project: lightning-python Author: lightning-viz File: visualization.py License: MIT License | 5 votes |
def get_html(self): r = requests.get(self.get_embed_link(), auth=self.auth) return r.text