I get error when trying to access details about servers storage using API. I want to extract backup status which is state in json:
{
"storage": {
"access": "private",
"backup_rule": {},
"backups": {
"backup": []
},
"license": 0,
"part_of_plan": "",
"servers": {
"server": [
""
]
},
"size": ,
"state": "online",
"tier": "",
"title": "",
"type": "",
"uuid": "",
"zone": ""
}
}
When I execute this code:
bkpdet = requests.get('https://fffff.com/1.2/storage/08475', auth=HTTPBasicAuth('login', 'pass'))
bkpdet_json = bkpdet.json()
datastg = bkpdet.json()
print(datastg)
for sts in datastg['storage']:
bkpsts = sts['state']
print(bkpsts)
I get this error:
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
TypeError: string indices must be integers
How can I access state*?
The whole idea is to at the end get info about status using this code:
if bkpsts == "online":
print('Backup has been created.')
else bkpsts == "backuping":
print('Backup creation is in progress.')
else:
print(bkpdet.status_code)
I was searching but still cannot find what is wrong here.
When you use:
for sts in datastg['storage']:
sts will be a string key. You are trying to treat it like an dictionary.
If you just need the state value, you can access it directly:
datastg['storage']['state']
If you want to iterate all key value pairs under storage, you can use items() to both key and value.
for key, value in datastg['storage'].items():
print(key,":", value)
As #MarkMeyer advised I changed code like this:
bkpdet = requests.get('https://fffff.com/1.2/storage/08475', auth=HTTPBasicAuth('login', 'pass'))
bkpdet_json = bkpdet.json()
datastg = bkpdet.json()
bkpsts = datastg['storage']['state']
print(bkpsts)
It works perfectly!
The code below works
data = {
"storage": {
"access": "private",
"backup_rule": {},
"backups": {
"backup": []
},
"license": 0,
"part_of_plan": "",
"servers": {
"server": [
""
]
},
"size": 1,
"state": "online",
"tier": "",
"title": "",
"type": "",
"uuid": "",
"zone": ""
}
}
state = data['storage']['state']
print(state)
output
online
Related
I'm looking for a way to filter client information into variables that i can use to send emails. One of the variables im looking for is "nospam1#gmail.com"
Can somebody help me with this?
The code i tested is:
import json
with open('notion_data.json') as json_file:
data = json.load(json_file)
if [x for x in data['properties'] if x.get('plain_text')=='nospam1#gmail.com']:
print("IN")
else:
print("NOT")
The error i get:
Traceback (most recent call last):
File "C:\Users\stijn\PycharmProjects\notion\scrath_notion.py", line 13, in <module>
if [x for x in data['properties'] if x.get('plain_text')=='nospam1#gmail.com']:
~~~~^^^^^^^^^^^^^^
KeyError: 'properties'
Process finished with exit code 1
Data of the json file:
{
"object": "list",
"results": [
{
"object": "page",
"id": "a94f4f2d-b965-43db-a8bf-02c1453033ee",
"created_time": "2022-11-15T18:53:00.000Z",
"last_edited_time": "2022-11-15T18:58:00.000Z",
"created_by": {
"object": "user",
"id": "9b60ada0-dc62-441f-8c0a-e1668a878d0e"
},
"last_edited_by": {
"object": "user",
"id": "9b60ada0-dc62-441f-8c0a-e1668a878d0e"
},
"cover": null,
"icon": null,
"parent": {
"type": "database_id",
"database_id": "4279b28e-fd9d-4efd-b9f7-957699839dd4"
},
"archived": false,
"properties": {
"email_sender": {
"id": "CdJY",
"type": "rich_text",
"rich_text": [
{
"type": "text",
"text": {
"content": "nospam2#gmail.com",
"link": null
}}}}}
You have to dive through ALL of the intermediate objects. Assuming there are multiple results:
for result in data['results']:
texttype = result['properties']['email_sender']['type']
email = result['properties']['email_sender'][texttype][0]['text']['content']
if email == 'nospam2#gmail.com':
print("winner")
I would like to find a way to convert a string inside my JSON object into another JSON object (nested) with the use of Python and thus add a key to the values inside my string that are separated by the use of a ";".
My current JSON object:
{
"reference": "#############",
"messageContext": "",
"from": {
"number": "+#############",
"name": ""
},
"to": {
"number": "#############"
},
"message": {
"text": "12302;8;6245;d517666e-41ca-459a-9b35-c49386df537b;2;2;50.8447;-4.3614;2021-04-28T22:24:12.204Z;rec123;PRD",
"media": {
"mediaUri": "",
"contentType": "",
"title": ""
},
"custom": {}
},
"groupings": [
"",
"",
""
],
"time": "2021-05-02 14:03:22",
"timeUtc": "2021-05-02T12:03:22",
"channel": "Sms"
}
The format I would try to have is something like this, where I would change the text to an object while adding key values.
Result, which I try to obtain (Inside of text:" "):
{
"reference": ""#############",",
"messageContext": "",
"from": {
"number": "+"#############",",
"name": ""
},
"to": {
"number": ""#############","
},
"message": {
"text": {
"APSI": "12302",
"idVhl": 8,
"idDrv": 6245,
"GUID": "d517666e-41ca-459a-9b35-c49386df537b",
"idLne": 2,
"idSvc": 2,
"Lat": 50.8447,
"Lon": -4.3614,
"Timestamp": "2021-04-28T22:24:12.204Z",
"Rec": "rec123",
"Env": "PRD"
},
"media": {
"mediaUri": "",
"contentType": "",
"title": ""
},
"custom": {}
},
"groupings": [
"",
"",
""
],
"time": "2021-05-02 14:03:22",
"timeUtc": "2021-05-02T12:03:22",
"channel": "Sms"
}
Given your JSON object as input this function will return your expected output.
def transform(input):
text = input['message']['text']
text_list = text.split(';')
text_dict = {
'APSI': text_list[0],
'idVhl': int(text_list[1]),
'idDrv': int(text_list[2]),
'GUID': text_list[3],
'idLne': int(text_list[4]),
'idSvc': int(text_list[5]),
'Lat': float(text_list[6]),
'Lon': float(text_list[7]),
'Timestamp': text_list[8],
'Rec': text_list[9],
'Env': text_list[10]
}
input['message']['text'] = text_dict
return input
This is done by splitting your text on the ; character which returns a list of all the values you want. You can then parse the list into a dictionary and then assign that dictionary to your original text value.
I have a banch of servers and I use API to extract data about storage.
To get details about storage uuid (named storage in json) I use code as below:
srvdet = requests.get('https://hhh.com/1.2/server/76856585', auth=HTTPBasicAuth('login', 'pass'))
srvdet_json = srvdet.json()
datas = srvdet.json()
print(datas)
And result in json:
{
"server": {
"boot_order": "",
"core_number": "2",
"created": ,
"firewall": "off",
"host": ,
"hostname": "hello",
"ip_addresses": {
"ip_address": [
{
"access": "private",
"address": "",
"family": ""
},
{
"access": "",
"address": "",
"family": "",
"part_of_plan": ""
}
]
},
"license": 0,
"memory_amount": "",
"nic_model": "",
"plan": "",
"plan_ipv4_bytes": "",
"plan_ipv6_bytes": "0",
"state": "started",
"storage_devices": {
"storage_device": [
{
"address": "",
"boot_disk": "0",
"part_of_plan": "",
"storage": "09845",
"storage_size": ,
"storage_title": "",
"type": ""
}
For now it works perfectly fine. The problem is when I need to get "09845" which is value for storage. When I try to use this code:
for storage in datas['server']['storage_devices']['storage_device']:
srvstorage = storage
print(srvstorage)
The result is:
{'storage': '09845', 'storage_size':, 'address': '', 'boot_disk': '0', 'type': '', 'part_of_plan': 'yes', 'storage_title': ''}
What am I doing wrong? How to save "09845" in variable?
EDIT:
Now I get error when trying to access details about storage.
I want to extract backup status which is state in json:
{
"storage": {
"access": "private",
"backup_rule": {},
"backups": {
"backup": []
},
"license": 0,
"part_of_plan": "",
"servers": {
"server": [
""
]
},
"size": ,
"state": "online",
"tier": "",
"title": "",
"type": "",
"uuid": "",
"zone": ""
}
}
When I execute this code:
bkpdet = requests.get('https://fffff.com/1.2/storage/08475', auth=HTTPBasicAuth('login', 'pass'))
bkpdet_json = bkpdet.json()
datastg = bkpdet.json()
print(datastg)
for sts in datastg['storage']:
bkpsts = sts['state']
print(bkpsts)
I get this error:
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
TypeError: string indices must be integers
The whole idea is to at the end get info about status using this code:
if bkpsts == "online":
print('Backup has been created.')
else bkpsts == "backuping":
print('Backup creation is in progress.')
else:
print(bkpdet.status_code)
I was searching but still can't find what is wrong here.
You miss a key when you want to access storage, you loop over it and it's fine. But in each iteration you get a dictionary from which you need to call the right key, in your case storage.
for storage in datas['server']['storage_devices']['storage_device']:
srvstorage = storage.get("storage", None)
print(srvstorage)
Note
get method will be preferable to use as you may encounter a device with out storage information inside, by using get you can avoid the KeyError.
You can just do
for device in datas['server']['storage_devices']['storage_device']:
device_storage = device['storage']
print(device_storage)
I have a function in python which is fetching the data in JSON format and I need to get the value of one item and store it in variable so I could use it another function
import requests
import json
import sys
def printResponse(r):
print '{} {}\n'.format(json.dumps(r.json(),
sort_keys=True,
indent=4,
separators=(',', ': ')), r)
r = requests.get('https://wiki.tourist.com/rest/api/content',
params={'title' : 'Release Notes for 1.1n1'},
auth=('ABCDE', '*******'))
printResponse(r)
getPageid = json.loads(r)
value = int(getPageid['results']['id'])
I am trying to get the value of id(160925) item in variable "value" so I could use it another function
Below is the JSON OUTPUT
{
"_links": {
"base": "https://wiki.tourist.com",
"context": "",
"self": "https://wiki.tourist.com/rest/api/content?title=Notes+for+1.1u1"
},
"limit": 25,
"results": [
{
"_expandable": {
"ancestors": "",
"body": "",
"children": "/rest/api/content/160925/child",
"container": "",
"descendants": "/rest/api/content/160925/descendant",
"history": "/rest/api/content/160925/history",
"metadata": "",
"operations": "",
"space": "/rest/api/space/Next",
"version": ""
},
"_links": {
"self": "https://wiki.tourist.com/rest/api/content/160925412",
"tinyui": "/x/5IaXCQ",
"webui": "/display/Next/Notes+for+1.1u1"
},
"extensions": {
"position": "none"
},
"id": "160925",
"status": "current",
"title": "Notes for 1.1u1",
"type": "page"
}
],
"size": 1,
"start": 0
} <Response [200]>
It looks like the "results" key in the JSON response corresponds to a list, so you'll want to index into that list to get a dict.
E.g. getPageid['results'][0]['id'] should return you the string value of the "id" key for the first object in the "results" list
Here's an example of the JSON I'm pulling from a URL:
[
{
"externalModelId": "500A000000RQOwnIAH",
"resource": {
"account": {
"externalModelId": "001A000001EucpoIAB",
"resource": {
"accountName": "Foobar",
"accountNumber": 1234567,
},
"resourceReliability": "Fresh"
},
"caseNumber": 1234567,
"created": "2015-06-12T19:06:22.000Z",
"createdBy": {
"externalModelId": "005A0000005mhdXIAQ",
"resourceReliability": "Fresh"
},
"description": "Example description",
"hoursInCurrentStatus": 406,
"internalPriority": "3 (Normal)",
"lastModified": "2015-06-22T14:08:18.000Z",
"owner": {
"externalModelId": "005A0000001sKDzIAM",
"resourceReliability": "Fresh"
},
"product": {
"resource": {
"line": {
"externalModelId": 21118,
"resource": {
"name": null
},
"resourceReliability": "Fresh"
},
"version": {
"externalModelId": 21988,
"resource": {
"name": "1.2"
},
"resourceReliability": "Fresh"
}
},
"resourceReliability": "Fresh"
},
"resourceCount": 0,
"sbrs": [
"Value"
],
"sbt": 139,
"severity": "4 (Low)",
"status": "Status Example",
"subject": "Subject Example",
"tags": [
"br",
"fs"
],
"targetDate": "2015-07-15T17:46:48.000Z",
"type": "Feature"
},
"resourceReliability": "Fresh"
},
I'm interested in pulling the following values from it:
caseNumber
subject
severity
sbt
sbrs
status
The code I currently have is:
#!/usr/bin/env python
import sys
import requests
import json
import os
# Setup
username = "XXX"
password = "XXX"
accountid = "12345"
# Formulate the string and then capture the output
url = "http://XXX{0}XXX{1}XXXXX".format(accountid, filedtime)
r = requests.get(url, auth=(username, password))
parsed = json.loads(r.text)
parent = parsed['resource']
# Using json_string for testing
#json_string = json.dumps(parsed, indent=4, sort_keys=True)
#print json_string
for item in parent:
print item['caseNumber']
print item['subject']
print item['severity']
print item['sbt']
print item['sbrs']
print item['status']
The code outputs a TypeError:
Traceback (most recent call last):
File "./newcase-notify.py", line 31, in <module>
parent = parsed['resource']
TypeError: list indices must be integers, not str
I've tried specifying something like:
parent = parsed['resource'][0]['type']
but that doesn't work. I think I'm confused at this point. If I don't specify a parent and simply iterate through 'parsed' like:
for item in parsed:
print item['caseNumber']
print item['subject']
print item['severity']
print item['sbt']
print item['sbrs']
print item['status']
I get KeyError's again.
My Question:
Given the information provided, how can I pull the above mentioned values from my JSON object?
I solved this by removing:
parent = parsed['resource']
and using:
for item in parsed:
print item['resource']['caseNumber']
print item['resource']['subject']
print item['resource']['severity']
etc.
If you look at the top of your JSON you'll notice this:
[
{
That means an array, with an object inside. You need to dereference that object form the array first. Hence why you're getting that jazz about list indices must be of type integer and not string. Once you do that it should work.
parent = parsed[0]['resource'] should fix you right up.
Just to help guide you with translating between the nomenclatures:
Array:JS as List:Python and Object:JS as Dict:Python.