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)
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 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
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
This question already has answers here:
Accessing elements of Python dictionary by index
(11 answers)
Closed 6 years ago.
When you have a JSON response that contains multiple JSON objects, how do you pull out a specific object within the JSON using Python?
For example, with the following JSON response, I have three objects in it.
{
"_links": {
"base": "REDACTED",
"context": "",
"self": "REDACTED"
},
"limit": 20,
"results": [
{
"_expandable": {
"ancestors": "",
"body": "",
"children": "",
"container": "",
"descendants": "",
"extensions": "",
"history": "/rest/api/content/198121503/history",
"metadata": "",
"operations": "",
"space": "/rest/api/space/ReleaseNotes",
"version": ""
},
"_links": {
"self": "REDACTED",
"tinyui": "/x/HxjPCw",
"webui": "UNIQUE_URL_HERE"
},
"id": "198121503",
"status": "current",
"title": "Unique Title of Content",
"type": "page"
},
{
"_expandable": {
"ancestors": "",
"body": "",
"children": "",
"container": "",
"descendants": "",
"extensions": "",
"history": "/rest/api/content/197195923/history",
"metadata": "",
"operations": "",
"space": "/rest/api/space/ReleaseNotes",
"version": ""
},
"_links": {
"self": "REDACTED",
"tinyui": "/x/k-jACw",
"webui": "UNIQUE_URL_HERE"
},
"id": "197195923",
"status": "current",
"title": "Unique Title of Content",
"type": "page"
},
{
"_expandable": {
"ancestors": "",
"body": "",
"children": "",
"container": "",
"descendants": "",
"extensions": "",
"history": "/rest/api/content/198121203/history",
"metadata": "",
"operations": "",
"space": "/rest/api/space/ReleaseNotes",
"version": ""
},
"_links": {
"self": "REDACTED",
"tinyui": "/x/8xbPCw",
"webui": "UNIQUE_URL_HERE"
},
"id": "198121203",
"status": "current",
"title": "Unique Title of Content",
"type": "page"
}
],
"size": 3,
"start": 0
}
How can I retrieve the ID and TITLE for a specific object in the response?
I read in other threads that when you use json.loads(your_json), it becomes a dictionary. If that's the case, how do I pull this data if it's stored as a dictionary?
Update
Let me clarify, as maybe I'm not seeing or explaining this clearly.
Is the only option to cycle through everything? There's not an option to say get me the 2nd JSON object and return the ID and Title? If that's the case, why shouldn't I create a custom object, store the items I want from each JSON object into those within an array, then I can access each object within the array?
After you transform your response to json, you can just use key attributes.
for result in data['results']:
print("id: {}, title: {}".format(result['id'], result['title']))
As you mentioned, you can use json.load to transform string to dictionary. But if you're using requests library, just use response.json to get data in required format.
Use bracket notation to access the keys after loading the string into a json object. Loop through the results key until you find the object you want, just like this:
j = json.loads(your_json)
for r in j["results"]:
if r["title"] == "Something":
print(r["id"])
print(r["title"])