First of all I have search the similar issues of mine but none able to answer my question above. I hope you guys could advise me further.
I' running a script to extract data from a list of network equipment and save the value onto json file for example below
json-1 = {
"channel": "scanner",
"action": "create_device",
"table": "U2",
"device":[]
}
data = "device_name","ip_address","lldp_neighbors"
Then a line of code is used to get data of devicename,ipaddress and lldp, return the value, extract it and save it onto the data list above. For example
my[data[0]] = connection.find_prompt().rstrip('>') #to get hostname
my[data[1]] = device['ip'] #to get ip address
my[data[2]] = connection.send_command('show lldp neighbors | display xml')
#to get lldp data in xml format
json1["device"].append(my) #append my data to device
For my[data[2]], lldp neighbors will return data in xml format and convert that xml data onto json format file like below
LLDP NEIGHBORS DETAILS:-
"lldp_neighbors": [
{
"local-port": "xe-3/0/4.0",
"parent-interface": "ae31.0",
"chassis-id": "b0:c6:9a:63:80:40",
"port-info": "xe-0/0/0/0.0",
"system-name": "host.jnpr.net"
}
My questions here is how can i add lldp neighbors detail above (json data) onto temp[data[2]] of json-1 so that the final json file json.dump(json-1, fp) generated will be like below, nested json file
{
"channel": "scanner",
"action": "create_device",
"table": "U2",
"device": [
{
"device_name": "rtr1.wer",
"ip_address": "1.1.1.1",
"lldp_neighbors": [
{
"local-port": "xe-3/0/4.0",
"parent-interface": "ae31.0",
"chassis-id": "b0:c6:9a:63:80:40",
"port-info": "xe-0/0/0/0.0",
"system-name": "host.jnpr.net"
}
]
]
}
I really hope someone could point me to the right path...i'm stuck ...please assist me. Thank you.
Your data should be a dictionary type, now a tuple type
data = "device_name","ip_address","lldp_neighbors"
# change to
data = {"device_name": "","ip_address": "","lldp_neighbors": []}
my[data["device_name"]] = connection.find_prompt().rstrip('>') #to get hostname
my[data["ip_address"]] = device['ip'] #to get ip address
my[data["lldp_neighbors"]] = connection.send_command('show lldp neighbors | display xml')
Related
I want to write a program that will save information from the API, in the form of a JSON file. The API has the 'exchangeId' parameter. When I save information from the API, I want to save only those files in which the 'exchangeId' will be different and his value will be more then one. How can I make it? Please, give me hand.
My Code:
exchangeIds = {102,311,200,302,521,433,482,406,42,400}
for pair in json_data["marketPairs"]:
if (id := pair.get("exchangeId")):
if id in exchangeIds:
json_data["marketPairs"].append(pair)
exchangeIds.remove(id)
pairs.append({
"exchange_name": pair["exchangeName"],
"market_url": pair["marketUrl"],
"price": pair["price"],
"last_update" : pair["lastUpdated"],
"exchange_id": pair["exchangeId"]
})
out_object["name_of_coin"] = json_data["name"]
out_object["marketPairs"] = pairs
out_object["pairs"] = json_data["numMarketPairs"]
name = json_data["name"]
Example of ExchangeIds output, that I don't need:
{200} #with the one id in `ExchangeId`
Example of JSON output:
{
"name_of_coin": "Pax Dollar",
"marketPairs": [
{
"exchange_name": "Bitrue",
"market_url": "https://www.bitrue.com/trade/usdp_usdt",
"price": 1.0000617355334473,
"last_update": "2021-12-24T16:39:09.000Z",
"exchange_id": 433
},
{
"exchange_name": "Hotbit",
"market_url": "https://www.hotbit.io/exchange?symbol=USDP_USDT",
"price": 0.964348817699553,
"last_update": "2021-12-24T16:39:08.000Z",
"exchange_id": 400
}
],
"pairs": 22
} #this one of exapmle that I need, because there are two id
I'm working in Python (3.8) and I've successfully called an API gotten it to print the JSON within command line after running the Python file. Now, I want to be able to print a particular list of information (like all of the names from the JSON), and later on save that list as its own set of data, but I'm hitting a block.
Example JSON I'm working with:
{
"data": {
"employees": [
{
"fields": {
"name": "Buddy",
"superheroName": "Syndrome",
"workEmail": "syndrome#example.com",
}
},
{
"fields": {
"name": "Helen Parr",
"superheroName": "Elastigirl",
"workEmail": "elastigirl#example.com",
}
}
]
}
I’ve tried the following so far and I was able to get “data” to print, but anytime I try to print another “layer” and get to say...“employees” or “fields” even, I hit a wall.
url = "my API url"
response = requests.get(url)
if response.status_code != 200:
print('Error with status code {}'.format(response.status_code))
exit()
jsonResponse = response.json()
jsonPretty = json.dumps(jsonResponse, indent=4, sort_keys=True)
jsonDictionary = json.loads(jsonPretty)
keys = jsonDictionary.keys()
for key in jsonDictionary.keys():
print(key)
Ideally, could someone share insight into how I can access the 'name' JSON value and get Python to print it as a list like the following, for example:
Buddy
Helen Parr
JSON files are basically nested dictionaries. jsonDictionary only contains one key and one entry under that key: data and another dictionary with the rest your result respectively.
If you wanted to access the name fields specifically:
employeesDict = jsonDictionary['data']
feildsDictList = employeesDict['employees']
firstFieldsDict = fieldsDictList[0]
secondFieldsDict = fieldsDictList[1]
firstName = firstFieldsDict['name']
secondNAme = secondFieldsDict['name']
You can access it like this (make sure it's already a dictionary):
for i in h['data']['employees']:
print(i['fields']['name'])
This way you can access the names with i['fields']['name']
I've been trying to figure out a way to store proxy data in a json form, i know the easier way is to just take each proxy from the text box and save it to the file and then to access it i would just load the information from the file but i want to have groups that work with different types of IP's. Say for example one group uses the proxy IP from a certain provider and another group would use an IP from a different one, i would need to store the IP's in their respected groups which is why i think i need to create a json file to store each of the proxies in their own json array. What i'm having trouble with is adding the IP's to the json array as i am trying to loop over a transfer file with the IP's in them and then add it to the json array. As of now i tried this,
def save_proxy():
proxy = pooled_data_default.get('1.0', 'end-2c')
transfer_file = open('proxies.txt', 'w')
transfer_file.write(proxy)
transfer_file.close()
transfer_file1 = open('proxies.txt', 'r')
try:
with open('proxy_groups.txt', 'r+') as file:
proxy_group = json.load(file)
except:
proxy_group = []
total = []
for line in transfer_file1:
line = transfer_file1.readline().strip()
total.append(line)
proxy_group.append({
'group_name': pool_info.get(),
'proxy': [{
'proxy': total,
}]
}),
with open('proxy_groups.txt', 'w') as outfile:
json.dump(proxy_group, outfile, indent=4)
This doesn't work but it was my attempt at taking each line from the file and adding it to the json array dynamically. Any help is appreciated.
EDIT: this is what is being outputted:
[
{
"group_name": "Defualt",
"proxy": [
{
"proxy": [
"asdf",
""
]
}
]
}
]
This was the input
wdsa
asdf
sfs
It seems that it is only selecting the middle one of the 3. I thought that printing the list of them would work but it is still printing the middle and then a blank space at the end.
An example of my data is the input to the text box may be
wkenwwins:1000:username:password
uwhsuh:1000:username:password
2ewswsd:1000:username:password
gfrfccv:1000:username:password
the selected group which i may want to save this to could be called 'Default'. I select default and then clicking save should add these inputs to the seperate txt sheet called 'proxies.txt', which it does. From the text sheet i then want to loop through each line and append each line to the json data. Which it doesnt do, here it was i expect it to look like in json data
[
{
"group_name": "Defualt",
"proxy": [
{
"proxy": [
'ewswsd:1000:username:password',
'wkenwwins:1000:username:password',
'uwhsuh:1000:username:password'
]
}
]
}
]
So then say if i made 2 groups the json data txt file should look like this:
[
{
"group_name": "Defualt",
"proxy": [
{
"proxy": [
'ewswsd:1000:username:password',
'wkenwwins:1000:username:password',
'uwhsuh:1000:username:password'
]
}
]
}
]
[
{
"group_name": "Test",
"proxy": [
{
"proxy": [
'ewswsd:1000:username:password',
'wkenwwins:1000:username:password',
'uwhsuh:1000:username:password'
]
}
]
}
]
This is so i can access each group by only calling the group name.
You can simplify the save_proxy() as below:
def save_proxy():
proxy = pooled_data_default.get('1.0', 'end-1c')
# save the proxies to file
with open('proxies.txt', 'w') as transfer_file:
transfer_file.write(proxy)
# load the proxy_groups if exists
try:
with open('proxy_groups.txt', 'r') as file:
proxy_group = json.load(file)
except:
proxy_group = []
proxy_group.append({
'group_name': pool_info.get(),
'proxy': proxy.splitlines()
})
with open('proxy_groups.txt', 'w') as outfile:
json.dump(proxy_group, outfile, indent=4)
The output file proxy_groups.txt would look like below:
[
{
"group_name": "default",
"proxy": [
"wkenwwins:1000:username:password",
"uwhsuh:1000:username:password"
]
}
]
I am new to python. I am writing a code, where I need to read the json file, and dump some of it's data to a new json file.
Following is my code:
if vmName=='IGW':
with open(APIJSONPath+NwIntfJSONFileName) as f:
data=json.load(f)
for item in data['Interfaces']:
jdata=item
with open(NwIntfJSONPath+vmName+'.json','w') as c:
json.dump(jdata,c,indent=2)
Following is a small part of my json file data from which this code is supposed to retrieve the interface details(Interface name,IPAddress, PrefixLength, DefaultGateway) of eth0 and eth1:
{
"Interfaces": [{
"Name": "eth0",
"IPConfigurations": [{
"IPAddress": "10.0.3.7",
"PrefixLength": 24,
"DefaultGateway": "10.0.3.1",
"IsPrimary": true
}],
"Description0": "connected to cloudsimple network",
"IsPrimary": true
} ,
{
"Name": "eth1",
"IPConfigurations": [{
"IPAddress": "10.0.3.8",
"PrefixLength": 24,
"DefaultGateway": "10.0.3.1",
"IsPrimary": true
}],
"Description1": "connected to internet"
}
]
}
But the data that is getting dumped the new json file is:
{
"Name": "eth1",
"IPConfigurations": [
{
"PrefixLength": 24,
"IsPrimary": true,
"IPAddress": "10.0.3.8",
"DefaultGateway": "10.0.3.1"
}
],
"Description1": "connected to internet"
}
Only eth1 details is getting dumped, not the eth0. The dumped data is also in an unordered manner.
Can someone please help me figure out, where I am going wrong, and how to fix this two issues in my code? Thanks in advance.
If you need all content of data['Interfaces'] in your output json use the below snippet.
if vmName=='IGW':
with open(APIJSONPath+NwIntfJSONFileName) as f:
data=json.load(f)
with open(NwIntfJSONPath+vmName+'.json','w') as c:
json.dump(data['Interfaces'],c,indent=2)
In your example you are looping through data['Interfaces'] and jdata holds the last value of the list. That is why you are only getting the last element in the output json.
Note: Please see comments of ticked answer for resolution :)
I'm writing some python which accesses an API and the exports the server's response (JSON) to a CSV file. The JSON is nested.
This is the JSON response from the server (this is just a sample of the response):
{
"id":182774,
"website_id":307842,
"engine_provider":"Google",
"engine_name":"United Kingdom",
"keywords":[
{
"id":4464443,
"groups_id":[
44424
],
"name":"SMART E70 Interactive Display",
"positions":{
"2017-03-16":10
}
},
{
"id":4464442,
"groups_id":[
44424
],
"name":"SMART Podium SP518",
"positions":{
"2017-03-16":4
}
},
{
"id":4464441,
"groups_id":[
44424
],
"name":"SMART Board M680",
"positions":{
"2017-03-16":3
}
},
{
"id":4464338,
"groups_id":[
51168
],
"name":"NEC Lamps",
"positions":{
"2017-03-16":4
}
}
]
}
If the JSON is looking wrong, it's probably because I edited it wrong when sampling it for this post.
In Python I try to parse the query response and then write the nested fields to a CSV table like this:
parsedqueryresponse = queryresponse.json()
f = csv.writer(open(csvoutputpath, "wb+"))
f.writerow(["name", "positions", "id"])
for parsedqueryresponse in parsedqueryresponse:
f.writerow([parsedqueryresponse["keywords"]["name"],
parsedqueryresponse["keywords"]["positions"],
parsedqueryresponse["keywords"]["id"]])
When I run the script I get this error:
"line 146, in
f.writerow([parsedqueryresponse["keywords"]["name"],
TypeError: string indices must be integers"
Line 146 is this one (also referenced by the error message):
f.writerow([parsedqueryresponse["keywords"]["name"]
What am I doing wrong here? I tried changing the JSON fields to use ' instead of " but that didn't seem to make things any better...
Please try this,
import csv
import json
parsedqueryresponse = queryresponse.json()
f = csv.writer(open(csvoutputpath, "wb+"))
f.writerow(["name", "positions", "id"])
for entry in parsedqueryresponse["keywords"]:
f.writerow([entry["name"],
entry["positions"],
entry["id"]])
Output:
name,positions,id
SMART E70 Interactive Display,{2017-03-16: 10},4464443
SMART Podium SP518,{2017-03-16: 4},4464442
SMART Board M680,{2017-03-16: 3},4464441
NEC Lamps,{2017-03-16: 4},4464338
Please let me know in terns of any queries.