How to iterate through a nested list in python? - python

I want to iterate through a list that has a lot of dictionaries inside it. The json response I'm trying to iterate looks something like this:
user 1 JSON response:
[
{
"id": "333",
"name": "hello"
},
{
"id": "999",
"name": "hi"
},
{
"id": "666",
"name": "abc"
},
]
user 2 JSON response:
[
{
"id": "555",
"name": "hello"
},
{
"id": "1001",
"name": "hi"
},
{
"id": "26236",
"name": "abc"
},
]
This is not the actual JSON response but it is structured the same way. What I'm trying to do is to find a specific id and store it in a variable. The JSON response I'm trying to iterate is not organized and changes every time depending on the user. So I need to find the specific id which would be easy but there are many dictionaries inside the list. I tried iterating like this:
for guild_info in guilds:
for guild_ids in guild_info:
This returns the first dictionary which is id: 333. For example, I want to find the value 666 and store it in a variable. How would I do that?

What you have is a list of dictionaries.
When you run for guild_info in guilds: you will iterate through dictionaries, so here each guild_info will be a dictionary. Therefore simply take the key id like so: guild_info['id'].
If what you want to do is find the name corresponding to a specific id, you can use list comprehension and take its first element, as follows:
name = [x['name'] for x in guilds if x['id'] == '666'][0]

Here's a function that will search only until it finds the matching id and then return, which avoids checking further entries unnecessarily.
def get_name_for_id(user, id_to_find):
# user is a list, and each guild in it is a dictionary.
for guild in user:
if guild['id'] == id_to_find:
# Once the matching id is found, we're done.
return guild['name']
# If the loop completes without returning, then there was no match.
return None
user = [
{
"id": "333",
"name": "hello"
},
{
"id": "999",
"name": "hi"
},
{
"id": "666",
"name": "abc"
},
]
name = get_name_for_id(user, '666')
print(name)
name2 = get_name_for_id(user, '10000')
print(name2)
Output:
abc
None

This will create a loop which will iterate to the list of dictionaries.If you are looking for simple approach
for every_dictionary in List_of_dictionary:
for every_dictionary_item in every_dictionary.keys():
print(every_dictionary[every_dictionary_item])

Related

creating dictionary inside a dictionary for converting it into Json data

My workflow is, I'll be having some dictionaries in different files, I'll be calling them into a file, let's say demo.py. I have a variable temp_dict in demo.py, in which the dictionary from different files will get appended one by one.
Example ::::
{
"Action": None,
"Parameter": "abc",
"ParameterDescription": "def",
"ExpectedValue": "ghi",
{
"Extensions":"jkl",
"MappedData": "no",
"Parameters": "pqr",
"Labels": "Stu",
}
{
"Recorder": "abc",
"Diagnostics": "efg",
"AdditionalRemarks": ""
}
}
I want this type of structure, I need to append dictionaries inside a dictionary, how can I do that.
I will also provide the python code
# function to add data to JSON
def write_json(new_data, filename='report.JSON'):
# new_data is the dictioanries coming from other files, it will be converted into json and dump it into a file.
with open(filename, 'w') as f:
json_string=json.dumps(new_data)
f.write(json_string)
Thanks in advance
The data you've provided is not a valid python dictionary, nor valid JSON.
Dictionaries and JSON are key: value pairs. The value might be a nested dict/JSON, however in your example the nested dictionaries do not have a key.
However, something like this would work:
{
"Action": None,
"Parameter": "abc",
"ParameterDescription": "def",
"ExpectedValue": "ghi",
"YOU NEED SOME NAME HERE": {
"Extensions":"jkl",
"MappedData": "no",
"Parameters": "pqr",
"Labels": "Stu",
},
…
}
You might have been thinking of json objects/dicts inside arrays. There the dictionaries don't have to be named, but that's because they implicitly have a name - their index (position) in the ordered array
[
{
"name": "Faboor",
"type": "user"
},
{
"name": "prithvi"
"reputation": 19
}
]

Getting Values with Python from JSON Array with multiple entries

i have a question regarding getting a specific value out of a JSON array based on a value that the array has. This might be a little vague bet let me show you.
I have a results array in JSON format:
{
"result": [{
"id": "SomeID1",
"name": "NAME1"
},
{
"id": "SomeID2",
"name": "NAME2"
}
]
}
I always know the name, but the ID is subject to change. So what i want to do is get the ID value based on the name I give. I am not able to alter the JSON format as it is a result i get from an API call.
So when enter NAME1 the result should be "SomeID1"
One approach could be (if name is unique):
data={
"result": [{
"id": "SomeID1",
"name": "NAME1"
},
{
"id": "SomeID2",
"name": "NAME2"
}
]
}
known_name ="NAME1"
print(next(x['id'] for x in data["result"] if x["name"]==known_name))
If name is not unique:
for x in data["result"]:
if x['name'] == known_name:
print(x["id"])
or you could store them in a list
print([x['id'] for x in data["result"] if x["name"]==known_name])

How to add a string to JSON list in python

Here is my code sample:
try:
REST_Call = Session.get(CC_URL_REST) #Getting the session for a particular url.
REST_CALL = REST_Call.content #Retrieving the contents from the url.
JSON_Data = json.loads(REST_CALL) #Loading data as JSON.
Report_JSON.append(JSON_Data) #Appending the data to an empty list
The JSON data that is returned and appended to the 'Report_JSON' is:
[
{
"content": [
{
"id": 001,
"name": "Sample_Name",
"description": "Sample_description",
"status": "STARTED",
"source": null,
"user": "username"
}
},
],
I just want to add the below data in string format to the above JSON list:
{
"CronExpression": "0 1,30 4,5,6,7,8,9,10,11,12,13,14 ? * 2,3,4,5,6"
},
Sample code for the above string data:
Cron_Call = Session.get(Cron_URL_REST)
Cron_CALL = Cron_Call.content
Cron_Data = json.loads(Cron_CALL)
cron_value = Cron_Data["cronExpression"]
Report_JSON.append({
"CronExpression": cron_value
})
When trying to append it to the 'Report_JSON' this is the output I get:
[
{
"content": [
{
"id": 001,
"name": "Sample_Name",
"description": "Sample_description",
"status": "STARTED",
"source": null,
"user": "username"
}
},
],
{
"CronExpression": "0 1,30 4,5,6,7,8,9,10,11,12,13,14 ? * 2,3,4,5,6"
},
I'm trying to show both the data's under the same "content" tab unlike it being separate.
This is the result i'm trying to get:
{
"id": 001,
"name": "Sample_Name",
"description": "Sample_description",
"status": "STARTED",
"source": null,
"user": "username"
"CronExpression": "0 1,30 4,5,6,7,8,9,10,11,12,13,14 ? * 2,3,4,5,6"
},
Any ideas on how to implement it?
Loop over JSON_Data['content'] and add the new key to each of them.
Cron_Call = Session.get(Cron_URL_REST)
Cron_CALL = Cron_Call.content
Cron_Data = json.loads(Cron_CALL)
cron_value = Cron_Data["cronExpression"]
for x in JSON_DATA['content']:
x['CronExpression'] = cron_value
Here, Report_JSON is loaded as a list type in Python (JSON data can be interpreted by Python as either a list, if it is surrounded by [] square brackets, or a dict if it is surrounded by {} curly brackets).
When you call Report_JSON.append(), it will append a new item to the list. You are creating a new dictionary with a single key-value pair (CronExpression) and adding it to the list, which is why the two dictionaries are side-by-side.
What you should do instead is get the first item in the Report_JSON list, which is the dictionary; then ask for the value corresponding to the content key, which will be a list; then ask for the first item in that list, which will be the dictionary you want to modify (with keys id, name, description, etc.)
Modify that dictionary, then put it back in the list. Here's the code that will do that:
# Get first item in Report_JSON list
content_dict = Report_JSON[0]
# Get the value corresponding to the 'content' key, which is a list
content_value = content_dict['content']
# Get the first item in the list, which is the dict you want to modify
dict_to_modify = content_value[0]
# Now add your new key-value pair
dict_to_modify['CronExpression'] = "0 1,30 4,5,6,7 ..."
Or, to do it in one shot:
Report_JSON[0]['content'][0]['CronExpression'] = "0 1,30 4,5,6,7 ..."
UPDATE: If the "content" list has multiple items, you can iterate over each item in that list:
for content_dict in Report_JSON[0]['content']:
content_dict['CronExpression'] = "0 1,30 4,5,6,7 ..."
which will result in something like this:
[
{
"content": [
{
"id": 001,
"name": "Sample_Name",
"description": "Sample_description",
"status": "STARTED",
"source": null,
"user": "username",
"CronExpression": "0 1,30 4,5,6,7 ..."
},
{
"id": 002,
"name": "Another_Sample_Name",
"description": "Another_sample_description",
"status": "STARTED",
"source": null,
"user": "another_username",
"CronExpression": "0 1,30 4,5,6,7 ..."
},
]
},
],
UPDATE 2: If you are not interested in keeping the original structure and you want to strip everything up to and including the "content" key, you can just do this to start off:
Report_JSON = Report_JSON[0]['content']
and Report_JSON is now just the inner "content" list.

Extracting data from JSON depending on other parameters

What are the options for extracting value from JSON depending on other parameters (using python)? For example, JSON:
"list": [
{
"name": "value",
"id": "123456789"
},
{
"name": "needed-value",
"id": "987654321"
}
]
When using json_name["list"][0]["id"] it obviously returns 123456789. Is there a way to indicate "name" value "needed-value" so i could get 987654321 in return?
For example:
import json as j
s = '''
{
"list": [
{
"name": "value",
"id": "123456789"
},
{
"name": "needed-value",
"id": "987654321"
}
]
}
'''
js = j.loads(s)
print [x["id"] for x in js["list"] if x["name"] == "needed-value"]
The best way to handle this is to refactor the json as a single dictionary. Since "name" and "id" are redundant you can make the dictionary with the value from "name" as the key and the value from "id" as the value.
import json
j = '''{
"list":[
{
"name": "value",
"id": "123456789"
},{
"name": "needed-value",
"id": "987654321"
}
]
}'''
jlist = json.loads(j)['list']
d = {jd['name']: jd['id'] for jd in jlist}
print(d) ##{'value': '123456789', 'needed-value': '987654321'}
Now you can iterate the items like you normally would from a dictionary.
for k, v in d.items():
print(k, v)
# value 123456789
# needed-value 987654321
And since the names are now hashed, you can check membership more efficiently than continually querying the list.
assert 'needed-value' in d
jsn = {
"list": [
{
"name": "value",
"id": "123456789"
},
{
"name": "needed-value",
"id": "987654321"
}
]
}
def get_id(list, name):
for el in list:
if el['name'] == name:
yield el['id']
print(list(get_id(jsn['list'], 'needed-value')))
Python innately treats JSON as a list of dictionaries. With this in mind, you can call the index of the list you need to be returned since you know it's location in the list (and child dictionary).
In your case, I would use list[1]["id"]
If, however, you don't know where the position of your needed value is within the list, the you can run an old fashioned for loop this way:
for user in list:
if user["name"] == "needed_value":
return user["id"]
This is assuming you only have one unique needed_value in your list.

Remove duplicates from a list of nested dictionaries

I'm writing my first python program to manage users in Atlassian On Demand using their RESTful API. I call the users/search?username= API to retrieve lists of users, which returns JSON. The results is a list of complex dictionary types that look something like this:
[
{
"self": "http://www.example.com/jira/rest/api/2/user?username=fred",
"name": "fred",
"avatarUrls": {
"24x24": "http://www.example.com/jira/secure/useravatar?size=small&ownerId=fred",
"16x16": "http://www.example.com/jira/secure/useravatar?size=xsmall&ownerId=fred",
"32x32": "http://www.example.com/jira/secure/useravatar?size=medium&ownerId=fred",
"48x48": "http://www.example.com/jira/secure/useravatar?size=large&ownerId=fred"
},
"displayName": "Fred F. User",
"active": false
},
{
"self": "http://www.example.com/jira/rest/api/2/user?username=andrew",
"name": "andrew",
"avatarUrls": {
"24x24": "http://www.example.com/jira/secure/useravatar?size=small&ownerId=andrew",
"16x16": "http://www.example.com/jira/secure/useravatar?size=xsmall&ownerId=andrew",
"32x32": "http://www.example.com/jira/secure/useravatar?size=medium&ownerId=andrew",
"48x48": "http://www.example.com/jira/secure/useravatar?size=large&ownerId=andrew"
},
"displayName": "Andrew Anderson",
"active": false
}
]
I'm calling this multiple times and thus getting duplicate people in my results. I have been searching and reading but cannot figure out how to deduplicate this list. I figured out how to sort this list using a lambda function. I realize I could sort the list, then iterate and delete duplicates. I'm thinking there must be a more elegant solution.
Thank you!
The usernames are unique, right?
Does it have to be a list? Seems like an easy solution would be to make it a dict of dicts instead. Use the usernames as keys, and only the most recent version will be present.
If the values have to be ordered, there is an OrderedDict type you could look into: http://docs.python.org/2/library/collections.html#collections.OrderedDict
Let say it is what you got,
JSON = [
{
"name": "fred",
...
},
{
"name": "peter",
...
},
{
"name": "fred",
...
},
Convert this list of dict to a dict of dict will remove the duplicate, like so:
r = dict([(user['name'], user) for user in JSON])
In r you will only find one record of fred and peter each.

Categories