Iterating over each json in the array - python

I have a query for SQL from which I need to prepare for each line json which I will use as a payload json for HTTP request. This json I need to rebuild a little bit, because for some keys I need to add another level of json. This is not a problem.
The problem is that I don't know how to iterate over each such object/row in the json. I need to do a for each and output as payload to separate HTTP requests. At this point I have:
result = []
for row in rows:
d = dict()
d['id'] = row[40]
d['email'] = row[41]
d['additional_level'] = dict()
d['additional_level']['key'] = row[42]
result.append(d)
payload = json.dumps(result, indent=3)
At this point, print(payload) looks like this:
[
{
"id": 01,
"email": "someemail01#gmail.com"
"additional_level": {
"key": 10
}
},
{
"id": 02,
"email": "someemail02#gmail.com"
"additional_level": {
"key": 10
}
}
]
Now I want to make a separate payload json from each id and use in request http. How can I refer to them and how to make for loop to separately refer to each "object" separately?

You can dump each object separately:
for row in rows:
d = dict()
d['id'] = row[40]
d['email'] = row[41]
d['additional_level'] = dict()
d['additional_level']['key'] = row[42]
result.append(json.dumpsd, indent = 3)
And then iterate over them and use them individually:
for payload in result:
# Use payload for a request

Related

Remove fields from a json array in Python

Currently I have a function returning json via jsonify.
[
{
"hostname": "bla",
"ipaddress": "192.168.1.10",
"subnetmask": "255.255.255.0",
"iloip": "192.168.1.11"
}
]
I want to keep it in json format, but I want to only show the fields I choose (i.e. reduce it). For this example, I want hostname and ipaddress.
Thanks
You can use dict comprehension:
json_input = '''
[
{
"hostname": "bla",
"ipaddress": "192.168.1.10",
"subnetmask": "255.255.255.0",
"iloip": "192.168.1.11"
}
]
'''
desired_keys = {'hostname', 'ipaddress'}
json_filtered = json.dumps([{ k:v for (k,v) in d.items() if k in desired_keys}
for d in json.loads(json_input)])
print(json_filtered)
output:
'[{"hostname": "bla", "ipaddress": "192.168.1.10"}]'
I belive what you want to achieve can be done with the code given below:
import json
data_json = '{"hostname": "bla","ipaddress": "192.168.1.10","subnetmask": "255.255.255.0","iloip": "192.168.1.11"}'
data = json.loads(data_json)
chosen_fields = ['hostname', 'ipaddress']
for field in chosen_fields:
print(f'{field}: {data[field]}')
Output:
hostname: bla
ipaddress: 192.168.1.10
Here what we do is we parse the stringified version of the json using the python's json module (i.e. json.loads(...)). Next decide on the fields we want to access (i.e. chosen_fields). Finally we iterate through the field we want to reach and get the corresponding values of the fields. This leaves the original json unmodified as you wished. Hope this helps.
Or else if you want these fields as a reduced json object:
import json
data_json = '{"hostname": "bla","ipaddress": "192.168.1.10","subnetmask": "255.255.255.0","iloip": "192.168.1.11"}'
data = json.loads(data_json)
chosen_fields = ['hostname', 'ipaddress']
reduced_json = "{"
for field in chosen_fields:
reduced_json += f'"{field}": "{data[field]}", '
reduced_json = list(reduced_json)
reduced_json[-2] = "}"
reduced_json = "".join(reduced_json)
reduced = json.loads(reduced_json)
for field in chosen_fields:
print(f'"{field}": "{reduced[field]}"')
Output:
"hostname": "bla"
"ipaddress": "192.168.1.10"
If I understand you correctly:
import json
response = [
{
"hostname": "bla",
"ipaddress": "192.168.1.10",
"subnetmask": "255.255.255.0",
"iloip": "192.168.1.11",
}
]
desired_keys = ["hostname", "ipaddress"]
new_response = json.dumps(
[{key: x[key] for key in desired_keys} for x in response]
)
And now you have a new_response - valid JSON, with which you can continue to work on

python: json print root object names

I have a JSON String like this:
{
"Sommersprossen": {
"count": 5,
"lastSeenTime": 1586959168567
},
"inkognito": {
"count": 7,
"lastSeenTime": 1586901586361
},
"Marienkäfer": {
"count": 7,
"lastSeenTime": 1586958264090,
"difficulty": 0.8902439024390244,
"difficultyWeight": 82
},
"Zaun": {
"count": 8,
"lastSeenTime": 1586958848320
},
Now I want to print something like this: Sommersprossen, inkognito, Marienkäfer, Zaun.
But I don't know how to...
My existing Python code is:
url = "https://skribbliohints.github.io/German.json"
response = urllib.request.urlopen(url)
data = json.loads(response.read())
But the problem is I cant find a way to print the objects names.
A json is essentially just a dictionary. You can access it like any other python dictionary.
url = "https://skribbliohints.github.io/German.json"
response = urllib.request.urlopen(url)
data = json.loads(response.read())
roots = list(data.keys())
You can treat this json output as dictionary and print its Keys, for this exapmle you can simply perform it with:
data.keys()
Output:
dict_keys(['Sommersprossen', 'inkognito', 'Marienkäfer', 'Zaun'])
It’s a dict, so you can do:
“, “.join(data.keys())

Iterate / Loop thru a json file using python multiple times

Ive a json file,
{
"IGCSE":[
{
"rolename": "igcsesubject1",
"roleid": 764106550863462431
},
{
"rolename": "igcsesubject2",
"roleid": 764106550863462431
}
],
"AS":[
{
"rolename": "assubject1",
"roleid": 854789476987546
},
{
"rolename": "assubject2",
"roleid": 854789476987546
}
],
"A2":[
{
"rolename": "a2subject1",
"roleid": 854789476987856
},
{
"rolename": "a2subject2",
"roleid": 854789476987856
}
]
}
I want to fetch the keys [igcse, as, a2..] and then fetch the rolename and roleids under the specific keys. How do i do it?
Below is the python code for how i used to do it without the keys.
with open(fileloc) as f:
data = json.load(f)
for s in range(len(data)):
d1 = data[s]
rname = d1["rolename"]
rid = d1["roleid"]
any help would be appreciated :)
First you can have a list of keys, under which you will get them:
l = ['A1','A2']
Then iterate like this:
for x in data:
if x in l:
for y in range(len(data[x])):
print(j[x][y]['rolename'])
print(j[x][y]['roleid'])
hi you can use for and you will get the keys:
with open(fileloc) as f:
data = json.load(f)
for s in data:
d1 = data[s]
rname = d1["rolename"]
rid = d1["roleid"]
The following would work for what you need:
with open(file) as f:
json_dict = json.load(f)
for key in json_dict:
value_list = json_dict[key]
for item in value_list:
rname = item["rolename"]
rid = item["roleid"]
If you need to filter for specific keys in the JSON, you can have a list of keys you want to obtain and filter for those keys as you iterate through the keys (similar to Wasif Hasan's suggestion above).

For looping several thousands of Url apis and adding it to a list

Problem: The output of this code seems to be repeating alot of the same entries in the final list, thus making it exponentially longer.
The goal would be complete the query and the print the final list with all city within the region
[
{
"name": "Herat",
"id": "AF~HER~Herat"
}
]
[
{
"name": "Herat",
"id": "AF~HER~Herat"
},
{
"name": "Kabul",
"id": "AF~KAB~Kabul"
}
]
[
{
"name": "Herat",
"id": "AF~HER~Herat"
},
{
"name": "Kabul",
"id": "AF~KAB~Kabul"
},
{
"name": "Kandahar",
"id": "AF~KAN~Kandahar"
}
]
My goal is to to a get a list with cityID. I first to a GET request and parse the JSON response to get the country IDs to a list,
Second: I have a for loop, which will make another GET request for the region id, but i now need to add the country IDs to the api url. I do that by adding .format on the GET request. and iterate trough all the countries and there respective region IDs, i parse them and store them in a list.
Third: i have another for loop, which will make another GET request for the cityID that will loop trough all cities with the above Region ID list, and the respectively collect the cityID that i really need.
Code :
from requests.auth import HTTPBasicAuth
import requests
import json
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
def countries():
data = requests.get("https://localhost/api/netim/v1/countries/", verify=False, auth=HTTPBasicAuth("admin", "admin"))
rep = data.json()
a = []
for elem in rep['items']:
a.extend([elem.get("id","")])
print(a)
return a
def regions():
ids = []
for c in countries():
url = requests.get("https://localhost/api/netim/v1/countries/{}/regions".format(c), verify=False, auth=HTTPBasicAuth("admin", "admin"))
response = url.json()
for cid in response['items']:
ids.extend([cid.get("id","")])
data = []
for r in ids:
url = requests.get("https://localhost/api/netim/v1/regions/{}/cities".format(r), verify=False, auth=HTTPBasicAuth("admin", "admin"))
response = url.json()
data.extend([{"name":r.get("name",""),"id":r.get("id", "")} for r in response['items']])
print(json.dumps(data, indent=4))
return data
regions()
print(regions())
You will see thou output contains several copies of the same entry.
Not a programmer, not sure where am i getting it wrong
It looks as though the output you're concerned with might be due to the fact that you're printing data as you iterate through it in the regions() method.
Try to remove the line:
print(json.dumps(data, indent=4))?
Also, and more importantly - you're setting data to an empty list every time you iterate on an item in Countries. You should probably declare that variable before the initial loop.
You're already printing the final result when you call the function. So printing as you iterate only really makes sense if you're debugging & needing to review the data as you go through it.

Iterate through Json Data to get URL's

I'm trying to parse the following JSON data and get URL values using python Function.From the below JSON example I would like to get the URL from under the Jobs tag and store it in 2 arrays. 1 array will store URL that has color tag and other will store URL that do not have color tag. Once the 2 arrays are ready I would like to return these two arrays. I'm very new to python and need some help with this.
{
"_class":"com.cloudbees.hudson.plugins.folder.Folder",
"actions":[ ],
"description":"This is a TSG level folder.",
"displayName":"CONSOLIDATED",
"displayNameOrNull":null,
"fullDisplayName":"CONSOLIDATED",
"fullName":"CONSOLIDATED",
"name":"CONSOLIDATED",
"url":"https://cyggm.com/job/CONSOLIDATED/",
"healthReport":[
{
"description":"Projects enabled for building: 187 of 549",
"iconClassName":"icon-health-20to39",
"iconUrl":"health-20to39.png",
"score":34
}
],
"jobs":[
{
"_class":"com.cloudbees.hudson.plugins.folder.Folder",
"name":"yyfyiff",
"url":"https://tdyt.com/job/
CONSOLIDATED/job/yfiyf/"
},
{
"_class":"com.cloudbees.hudson.plugins.folder.Folder",
"name":"Ops-Prod-Jobs",
"url":"https://ygduey.com/job/
CONSOLIDATED/job/Ops-Prod-Jobs/"
},
{
"_class":"com.cloudbees.hudson.plugins.folder.Folder",
"name":"TEST-DATA-MGMT",
"url":"https://futfu.com/job/
CONSOLIDATED/job/TEST-DATA-MGMT/"
},
{
"_class":"com.cloudbees.hudson.plugins.folder.Folder",
"name":"TESTING-OPS",
"url":"https://gfutfu.com/job/
CONSOLIDATED/job/TESTING-OPS/"
},
{
"_class":"com.cloudbees.hudson.plugins.folder.Folder",
"name":"Performance_Engineering Team",
"url":"https://ytdyt.com/job/
CONSOLIDATED/job/Performance_Engineering%20Team/"
},
{
"_class":"hudson.model.FreeStyleProject",
"name":"test",
"url":"https://tduta.com/job/
CONSOLIDATED/job/test/",
"color":"notbuilt"
}
],
"primaryView":{
"_class":"hudson.model.AllView",
"name":"all",
"url":"https://fuyfi.com/job/
CONSOLIDATED/"
},
"views":[
{
"_class":"hudson.model.AllView",
"name":"all",
"url":"https://utfufu.com/job/
CONSOLIDATED/"
}
]
}
The following is the python code I used to get the jobs data but then I'm not able to iterate through the jobs data to get all URL. I'm only getting 1 at a time if I change the code
req = requests.get(url, verify=False, auth=(username, password))
j = json.loads(req.text)
jobs = j['jobs']
print(jobs[1]['url'])
I'm getting 2nd URL here but no way to check if this entry has color tag
First of all, your JSON is improperly formatted. You will have to use a JSON formatter to check its validity and fix any issues.
That said, you'll have to read in the file as a string with
In [87]: with open('data.json', 'r') as f:
...: data = f.read()
...:
Then using the json library, load the data into a dict
In [88]: d = json.loads(data)
You can then use 2 list comprehensions to get the data you want
In [90]: no_color = [record['url'] for record in d['jobs'] if 'color' not in record]
In [91]: color = [record['url'] for record in d['jobs'] if 'color' in record]
In [93]: no_color
Out[93]:
['https://tdyt.com/job/CONSOLIDATED/job/yfiyf/',
'https://ygduey.com/job/CONSOLIDATED/job/Ops-Prod-Jobs/',
'https://futfu.com/job/CONSOLIDATED/job/TEST-DATA-MGMT/',
'https://gfutfu.com/job/CONSOLIDATED/job/TESTING-OPS/',
'https://ytdyt.com/job/CONSOLIDATED/job/Performance_Engineering%20Team/']
In [94]: color
Out[94]: ['https://tduta.com/job/CONSOLIDATED/job/test/']

Categories