How to get output into dictionary format from given data? - python

I have a field where value is as below
b = [
{
"category": "Engineer",
"name": "Anthony Test",
"id": 219
},
{
"category": "Engineer",
"name": "Pete Junior",
"id": 220
}
]
I would like to get the result in below format where I want to eliminate category and create a dictionary with id and name
result = {'219': 'Anthony Test', '220': 'Pete Junior'}

You can use dictionary comprehension
{i['id']: i['name'] for i in b}

Related

Find a value in a list of dictionaries

I have the following list:
{
"id":1,
"name":"John",
"status":2,
"custom_attributes":[
{
"attribute_code":"address",
"value":"st"
},
{
"attribute_code":"city",
"value":"st"
},
{
"attribute_code":"job",
"value":"test"
}]
}
I need to get the value from the attribute_code that is equal city
I've tried this code:
if list["custom_attributes"]["attribute_code"] == "city" in list:
var = list["value"]
But this gives me the following error:
TypeError: list indices must be integers or slices, not str
What i'm doing wrong here? I've read this solution and this solution but din't understood how to access each value.
Another solution, using next():
dct = {
"id": 1,
"name": "John",
"status": 2,
"custom_attributes": [
{"attribute_code": "address", "value": "st"},
{"attribute_code": "city", "value": "st"},
{"attribute_code": "job", "value": "test"},
],
}
val = next(d["value"] for d in dct["custom_attributes"] if d["attribute_code"] == "city")
print(val)
Prints:
st
Your data is a dict not a list.
You need to scan the attributes according the criteria you mentioned.
See below:
data = {
"id": 1,
"name": "John",
"status": 2,
"custom_attributes": [
{
"attribute_code": "address",
"value": "st"
},
{
"attribute_code": "city",
"value": "st"
},
{
"attribute_code": "job",
"value": "test"
}]
}
for attr in data['custom_attributes']:
if attr['attribute_code'] == 'city':
print(attr['value'])
break
output
st

Very nested JSON with optional fields into pandas dataframe

I have a JSON with the following structure. I want to extract some data to different lists so that I will be able to transform them into a pandas dataframe.
{
"ratings": {
"like": {
"average": null,
"counts": {
"1": {
"total": 0,
"users": []
}
}
}
},
"sharefile_vault_url": null,
"last_event_on": "2021-02-03 00:00:01",
],
"fields": [
{
"type": "text",
"field_id": 130987800,
"label": "Name and Surname",
"values": [
{
"value": "John Smith"
}
],
{
"type": "category",
"field_id": 139057651,
"label": "Gender",
"values": [
{
"value": {
"status": "active",
"text": "Male",
"id": 1,
"color": "DCEBD8"
}
}
],
{
"type": "category",
"field_id": 151333010,
"label": "Field of Studies",
"values": [
{
"value": {
"status": "active",
"text": "Languages",
"id": 3,
"color": "DCEBD8"
}
}
],
}
}
For example, I create a list
names = []
where if "label" in the "fields" list is "Name and Surname" I append ["values"][0]["value"] so names now contains "John Smith". I do exactly the same for the "Gender" label and append the value to the list genders.
The above dictionary is contained in a list of dictionaries so I just have to loop though the list and extract the relevant fields like this:
names = []
genders = []
for r in range(len(users)):
for i in range(len(users[r].json()["items"])):
for field in users[r].json()["items"][i]["fields"]:
if field["label"] == "Name and Surname":
names.append(field["values"][0]["value"])
elif field["label"] == "Gender":
genders.append(field["values"][0]["value"]["text"])
else:
# Something else
where users is a list of responses from the API, each JSON of which has the items is a list of dictionaries where I can find the field key which has as the value a list of dictionaries of different fields (like Name and Surname and Gender).
The problem is that the dictionary with "label: Field of Studies" is optional and is not always present in the list of fields.
How can I manage to check for its presence, and if so append its value to a list, and None otherwise?
To me it seems that the data you have is not valid JSON. However if I were you I would try using pandas.json_normalize. According to the documentation this function will put None if it encounters an object with a label not inside it.

parse specific data from the list using list comprehension

i am facing the problem which is to parse specific data from list. I have a list sth like :
response_list = [{
"Name": "Brand",
"Value": "Smart Planet",
"Source": "ItemSpecific"
},
{
"Name": "Color",
"Value": "Yellow",
"Source": "ItemSpecific"
},
{
"Name": "Type",
"Value": "Sandwich Maker",
"Source": "ItemSpecific"
},
{
"Name": "Power Source",
"Value": "Electrical",
"Source": "ItemSpecific"
}]
From the list I should get the Brand name. Every time it will come in different position.How can i get it using list comprehension.Output will be like :
new_list =[{'Brand':'Smart Planet'}]
You have a list of dicts, you want to take only the Name and the Value field of this list.
You can do:
[{item["Name"]: item["Value"]} for item in response_list]
You get:
[{'Brand': 'Smart Planet'}, {'Color': 'Yellow'}, {'Type': 'Sandwich Maker'}, {'Power Source': 'Electrical'}]
Is it what you want?
EDIT
If you only want the brand name, you need to filter:
[{"Brand": item["Value"]} for item in response_list if item["Name"] == "Brand"]

Group and sort JSON array of dictionaries by repeatable keys in Python

I have a json that is a list of dictionaries that looks like this:
I am getting it from MySQL with pymysql
[{
"id": "123",
"name": "test",
"group": "test_group"
},
{
"id": "123",
"name": "test",
"group": "test2_group"
},
{
"id": "456",
"name": "test2",
"group": "test_group2"
},
{
"id": "456",
"name": "test2",
"group": "test_group3"
}]
I need to group it so each "name" will have just one dict and it will contain a list of all groups that under this name.
something like this :
[{
"id": "123",
"name": "test",
"group": ["test2_group", "test_group"]
},
{
"id": "456",
"name": "test2",
"group": ["test_group2", "test_group3"]
}]
I would like to get some help,
Thanks !
You can use itertools.groupby for grouping of data.
Although I don't guarantee solution below to be shortest way but it should do the work.
# Your input data
data = []
from itertools import groupby
res = []
key_func = lambda k: k['id']
for k, g in groupby(sorted(data, key=key_func), key=key_func):
obj = { 'id': k, 'name': '', 'group': []}
for group in g:
if not obj['name']:
obj['name'] = group['name']
obj['group'].append(group['group'])
res.append(obj)
print(res)
It should print the data in required format.

Get Value of a key from List of dictionary

I have a list of dictionaries with 2 keys: 'name' & 'value'. I need to get the value of 'value' key if value of 'name' key is 'Subject'
Currently i'm iterating through each dict and checking the value of 'name' and getting the value of 'value'. Is there any improved way of doing this ?
items = [
{
"name": "From",
"value": "from#example.com"
},
{
"name": "Date",
"value": "Tue, 8 Sep 2014 16:18:35 +0530"
},
{
"name": "Subject",
"value": "Test Subject"
},
{
"name": "To",
"value": "sender#example.com"
},
]
for item in items:
if item.get('name') == 'Subject':
print "Subject: " + item.get('value')
You should transpose your data into a dict instead:
>>> d = dict([(e['name'],e['value']) for e in items]) # transpose
Or a slightly easier way as mentioned by Peter Wood in the comments:
>>> d = {e['name']: e['value'] for e in items}
Then just use the dict as normal:
>>> d['Subject']
'Test Subject'
You could use next along with a generator filtering and converting the items.
>>> subject = next(field['value']
... for field in items
... if field['name'] == 'Subject')
As Jonas told, better for modify your structure because
from collections import defaultdict
it = [
{
"name": "From",
"value": "from#example.com"
},
{
"name": "Date",
"value": "Tue, 8 Sep 2014 16:18:35 +0530"
},
{
"name": "Subject",
"value": "Test Subject"
},
{
"name": "Subject",
"value": "Test Subject 55"
},
{
"name": "To",
"value": "sender#example.com"
},
]
result = defaultdict(list)
# shorcut better to use for...
[result[item["name"]].append(item["value"]) for item in it]
print(result["Subject"])
['Test Subject', 'Test Subject 55']

Categories