Issues creating list from list of dictionaries in python - python

I have a file with json data in it like this:
data = [
{
"id": 12345678,
"list_id": 12345,
"creator_id": 1234567,
"entity_id": 1234567,
"created_at": "2020-01-30T00:43:55.256-08:00",
"entity": {
"id": 123456,
"type": 0,
"first_name": "John",
"last_name": "Doe",
"primary_email": "john#fakemail.com",
"emails": [
"john#fakemail.com"
]
}
},
{
"id": 12345678,
"list_id": 12345,
"creator_id": 1234567,
"entity_id": 1234567,
"created_at": "2020-01-30T00:41:54.375-08:00",
"entity": {
"id": 123456,
"type": 0,
"first_name": "Jane",
"last_name": "Doe",
"primary_email": "jane#fakemail.com",
"emails": [
"jane#fakemail.com"
]
}
}
]
I managed to extract the "first_name" values as well as the "primary_email" with the following code
for record in data:
first_names = record.get('entity',{}).get('first_name', None)
email = record.get('entity',{}).get('primary_email', None)
print(first_names)
print(email)
which produces following output:
John
john#fakemail.com
Jane
jane#fakemail.com
I am struggling however to create two separate lists for names and email like this:
(John,Jane)
(john#fakemail.com,jane#fakemail.com)
Any help with this is much appreciated.

import json
first_names = [record.get('entity',{}).get('first_name', None) for record in data]
email = [record.get('entity',{}).get('primary_email', None) for record in data]
print(first_names)
print(email)
or in the same loop:
first_names = []
email = []
for record in data:
first_names.append(record.get('entity',{}).get('first_name', None))
email.append(record.get('entity',{}).get('primary_email', None))
print(first_names)
print(email)

Related

Loop over JSON elements in Python

I have a long JSON string, and I want to find matching names in other elements. Something like in which other elements appears "Bob" and "John" names or "Jacob" and "Max". I was thinking to loop first element names and try to find them in other elements and continue doing it until very last element. The matching names arrays, according on length sort in to different lists. However, I don't know to accomplish this in python. Please help me.
The JSON looks like fallowing:
[
{
"group": 01,
"names": [
"Bob",
"John",
"Alex",
"Jacob",
"Theo",
"Thomas",
"Max"
],
"status": "none"
},
{
"group": 02,
"names": [
"Martin",
"Bryan",
"Alex",
"Adam",
"Arlo",
"Luca",
"Ellis"
],
"status": "In Progress"
},
{
"group": 03,
"names": [
"Alex",
"John",
"Emma",
"Toby",
"Ryan",
"Leon",
"Blake"
],
"status": "Completed"
},
{
"group": 04,
"names": [
"John",
"Martin",
"Liam",
"Felix",
"Finn",
"Ollie",
"Elliot"
],
"status": "In Progress"
},
{
"group": 05,
"names": [
"Luke",
"Emma",
"Alex",
"Arlo",
"Finn",
"Bob",
"Theo"
],
"status": "In Progress"
}
]
This example will use the json module to load the Json data from a file and then filter the items to only those which contain "Bob" and "John":
import json
with open("data.json", "r") as f_in:
data = json.load(f_in)
# find elements which contain "Bob" and "John"
out = []
for d in data:
if "Bob" in d["names"] and "John" in d["names"]:
out.append(d)
print(out)
Prints:
[
{
"group": 1,
"names": ["Bob", "John", "Alex", "Jacob", "Theo", "Thomas", "Max"],
"status": "none",
}
]

Finding duplicates from array using Python

I'm quite new to Python and was wondering if there was a good way to create a new list of unduplicated users.
My problem is that I have something like this
[
{
"userId": "987654321",
"method": "CARD",
"lastDigits": "1234",
"type": "mc",
"first_name": "Leroy",
"last_name": "Jenkins",
"exp": "01/23"
},
{
"userId": "987654321",
"method": "PAYPAL",
"first_name": "Leroy",
"last_name": "Jenkins"
},
{
"userId": "123456789",
"method": "CARD",
"lastDigits": "4567",
"type": "visa",
"first_name": "Joe",
"last_name": "Bloggs",
"exp": "01/25"
},
{
"userId": "46513498000",
"method": "PAYPAL",
"first_name": "Betty",
"last_name": "White"
}
]
Basically I need to match when the userId has matched and keep the object when "method": "CARD"instead of PAYPAL then reconstruct essentially the same list again but minus the duplicate userId's when the user has both CARD and PAYPAL
::EDIT::
User can just have PAYPAL. and if it does just have PAYPAL, just return that
example output needed
[
{
"userId": "987654321",
"method": "CARD",
"lastDigits": "1234",
"type": "mc",
"first_name": "Leroy",
"last_name": "Jenkins",
"exp": "01/23"
},
{
"userId": "123456789",
"method": "CARD",
"lastDigits": "4567",
"type": "visa",
"first_name": "Joe",
"last_name": "Bloggs",
"exp": "01/25"
},
{
"userId": "46513498000",
"method": "PAYPAL",
"first_name": "Betty",
"last_name": "White"
}
]
This will perfectly work for your. Try and check it
mylist=[
{
"userId": "987654321",
"method": "CARD",
"lastDigits": "1234",
"type": "mc",
"first_name": "Leroy",
"last_name": "Jenkins",
"exp": "01/23"
},
{
"userId": "987654321",
"method": "PAYPAL",
"first_name": "Leroy",
"last_name": "Jenkins"
},
{
"userId": "123456789",
"method": "CARD",
"lastDigits": "4567",
"type": "visa",
"first_name": "Joe",
"last_name": "Bloggs",
"exp": "01/25"
},
{
"userId": "46513498000",
"method": "PAYPAL",
"first_name": "Betty",
"last_name": "White"
}
]
temp_list=[]
temp_id=[]
for x in mylist:
if int(x['userId']) not in temp_id:
temp_list.append(x)
temp_id.append(int(x["userId"]))
print(temp_list)
If I don't misunderstood your question then simple filtering will do the trick for you,
user_ids = []
final_users = []
for user in users:
user_ids.append(int(user['userId']))
if user['userId'] not in user_ids and user['method'] != 'PAYPAL':
final_users.append(user)
print(final_users)
Working Code: https://rextester.com/COBGVU63922
users = {}
for d in user_list:
uid = d["userId"]
# If user id not in users, we add it
if uid not in users:
users[uid] = d
# Otherwise we check if the already recorded method was "PAYPAL",
# if so we overwrite it.
elif users[uid]["method"] == "PAYPAL":
users[uid] = d
# To convert dict we just created back to list:
user_list = list(users.values())
Use defaultdict:
from collections import defaultdict
newdata = defaultdict(dict)
for item in data:
userid = newdata[item['userId']]
if userid == {} and item['method'] == 'CARD':
userid.update(item)
Output:
# newdata = list(newdata.values())
>>> newdata
[{'userId': '987654321',
'method': 'CARD',
'lastDigits': '1234',
'type': 'mc',
'first_name': 'Leroy',
'last_name': 'Jenkins',
'exp': '01/23'},
{'userId': '123456789',
'method': 'CARD',
'lastDigits': '4567',
'type': 'visa',
'first_name': 'Joe',
'last_name': 'Bloggs',
'exp': '01/25'}]

How to create a function to parse a JSON block?

With the below 'food_object' json block, create a function to parse the 'food_object' and provide the two variables 'desired_type' and 'desired_id' that will return the corresponding food to be returned, given provided desired type and id. With the returned result, print out the result in a string with the 'desired_type' variable in a format like this: 'My fruit's name is: apple"
food_object = {
"fruit": [
{
"id": "001",
"name": "apple"
},{
"id": "002",
"name": "orange"
},{
"id": "003",
"name": "banana"
}
],
"vegetable": [
{
"id": "001",
"name": "carrot"
},{
"id": "002",
"name": "broccoli"
},{
"id": "003",
"name": "green beans"
}
]
}
desired_type = "fruit"
desired_id = "001"
You just need to parse the original string with json.loads and then process it like a normal dictionary.
I give you a fully functional example that resolves your concrete problem.
import json
# functions definition
def parse_json_payload(payload: str):
"""
Returns a object from string json payload
"""
parsed = json.loads(original_str)
return parsed
def extract_desired_type(object: dict,desired_type: str):
"""
Returns de desired object type
"""
return object[desired_type]
def extract_desired_id(objects, id: str):
"""
Returns the desired object id if exists.
If not, returns empty object
"""
for element in objects:
if element["id"] == id:
return element
return {}
# main
original_str = """{
"fruit": [
{
"id": "001",
"name": "apple"
},{
"id": "002",
"name": "orange"
},{
"id": "003",
"name": "banana"
}
],
"vegetable": [
{
"id": "001",
"name": "carrot"
},{
"id": "002",
"name": "broccoli"
},{
"id": "003",
"name": "green beans"
}
]
}
"""
def show_example_1():
# Extract fruit with id 001
body = parse_json_payload(original_str)
type_object = extract_desired_type(body, "fruit")
object = extract_desired_id(type_object,"001")
print(object)
def show_example_2():
# Extract vegetable with id 002
body = parse_json_payload(original_str)
type_object = extract_desired_type(body, "vegetable")
object = extract_desired_id(type_object,"002")
print(object)
show_example_1()
print("\n\n")
show_example_2()
Output example:
python3 json_parser.py
{'id': '001', 'name': 'apple'}
{'id': '002', 'name': 'broccoli'}
Use the function loads from the json module to convert a JSON string to a dictionary, then loop over the items of the list json[desired_type] and check for each item if it's id matches the desired_id, if so, return that item's name. If there's no match, return something like not found. Then, use a print to print out the result:
import json
desired_type = "fruit"
desired_id = "001"
json_string = """{
"fruit": [
{
"id": "001",
"name": "apple"
},{
"id": "002",
"name": "orange"
},{
"id": "003",
"name": "banana"
}
],
"vegetable": [
{
"id": "001",
"name": "carrot"
},{
"id": "002",
"name": "broccoli"
},{
"id": "003",
"name": "green beans"
}
]
}"""
jsonobj = json.loads(json_string)
def get_item(desired_type, desired_id):
for item in jsonobj[desired_type]:
if item["id"] == desired_id:
return item["name"]
return "not found"
print("My", desired_type, "'s name is:", get_item(desired_type, desired_id))

How to get a value from JSON

This is the first time I'm working with JSON, and I'm trying to pull url out of the JSON below.
{
"name": "The_New11d112a_Company_Name",
"sections": [
{
"name": "Products",
"payload": [
{
"id": 1,
"name": "TERi Geriatric Patient Skills Trainer,
"type": "string"
}
]
},
{
"name": "Contact Info",
"payload": [
{
"id": 1,
"name": "contacts",
"url": "https://www.a3bs.com/catheterization-kits-8000892-3011958-3b-scientific,p_1057_31043.html",
"contacts": [
{
"name": "User",
"email": "Company Email",
"phone": "Company PhoneNumber"
}
],
"type": "contact"
}
]
}
],
"tags": [
"Male",
"Airway"
],
"_id": "0e4cd5c6-4d2f-48b9-acf2-5aa75ade36e1"
}
I have been able to access description and _id via
data = json.loads(line)
if 'xpath' in data:
xpath = data["_id"]
description = data["sections"][0]["payload"][0]["description"]
However, I can't seem to figure out a way to access url. One other issue I have is there could be other items in sections, which makes indexing into Contact Info a non starter.
Hope this helps:
import json
with open("test.json", "r") as f:
json_out = json.load(f)
for i in json_out["sections"]:
for j in i["payload"]:
for key in j:
if "url" in key:
print(key, '->', j[key])
I think your JSON is damaged, it should be like that.
{
"name": "The_New11d112a_Company_Name",
"sections": [
{
"name": "Products",
"payload": [
{
"id": 1,
"name": "TERi Geriatric Patient Skills Trainer",
"type": "string"
}
]
},
{
"name": "Contact Info",
"payload": [
{
"id": 1,
"name": "contacts",
"url": "https://www.a3bs.com/catheterization-kits-8000892-3011958-3b-scientific,p_1057_31043.html",
"contacts": [
{
"name": "User",
"email": "Company Email",
"phone": "Company PhoneNumber"
}
],
"type": "contact"
}
]
}
],
"tags": [
"Male",
"Airway"
],
"_id": "0e4cd5c6-4d2f-48b9-acf2-5aa75ade36e1"
}
You can check it on http://json.parser.online.fr/.
And if you want to get the value of the url.
import json
j = json.load(open('yourJSONfile.json'))
print(j['sections'][1]['payload'][0]['url'])
I think it's worth to write a short function to get the url(s) and make a decision whether or not to use the first found url in the returned list, or skip processing if there's no url available in your data.
The method shall looks like this:
def extract_urls(data):
payloads = []
for section in data['sections']:
payloads += section.get('payload') or []
urls = [x['url'] for x in payloads if 'url' in x]
return urls
This should print out the URL
import json
# open json file to read
with open('test.json','r') as f:
# load json, parameter as json text (file contents)
data = json.loads(f.read())
# after observing format of JSON data, the location of the URL key
# is determined and the data variable is manipulated to extract the value
print(data['sections'][1]['payload'][0]['url'])
The exact location of the 'url' key:
1st (position) of the array which is the value of the key 'sections'
Inside the array value, there is a dict, and the key 'payload' contains an array
In the 0th (position) of the array is a dict with a key 'url'
While testing my solution, I noticed that the json provided is flawed, after fixing the json flaws(3), I ended up with this.
{
"name": "The_New11d112a_Company_Name",
"sections": [
{
"name": "Products",
"payload": [
{
"id": 1,
"name": "TERi Geriatric Patient Skills Trainer",
"type": "string"
}
]
},
{
"name": "Contact Info",
"payload": [
{
"id": 1,
"name": "contacts",
"url": "https://www.a3bs.com/catheterization-kits-8000892-3011958-3b-scientific,p_1057_31043.html",
"contacts": [
{
"name": "User",
"email": "Company Email",
"phone": "Company PhoneNumber"
}
],
"type": "contact"
}
]
}
],
"tags": [
"Male",
"Airway"
],
"_id": "0e4cd5c6-4d2f-48b9-acf2-5aa75ade36e1"}
After utilizing the JSON that was provided by Vincent55.
I made a working code with exception handling and with certain assumptions.
Working Code:
## Assuming that the target data is always under sections[i].payload
from json import loads
line = open("data.json").read()
data = loads(line)["sections"]
for x in data:
try:
# With assumption that there is only one payload
if x["payload"][0]["url"]:
print(x["payload"][0]["url"])
except KeyError:
pass

python json(how to read mulpite id in comple

I have json file i what to read all the values
data=""" {"employees":[
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter", "lastName":"Jones"}
]}
{
"maps":[
{"id":"apple","iscategorical":"0"},
{"id":"ball","iscategorical":"0"}
],
"mask":{"id1":"aaaaa"},
"mask":{"id1":"bbb"},
"mask":{"id1":"cccccc"},
"om_points":"value",
"parameters":
{"id":"valore"}
}"""
out = json.loads(data)
how to get all values
firstname
lastname
mask.id1
map.id
output:
[(firstname_vaues,lastname_values,mask.id1,map.id)
(firstname_vaues,lastname_values,mask.id1,map.id) ......]
please help me
First thing, there are two json objects in your data string. So you cannot use json.loads(data). You can seperate them by a charcter like ";" . Then split the string and use json.loads on each of them.Use following code.
import json
data=""" {
"employees": [{
"firstName": "John",
"lastName": "Doe"
}, {
"firstName": "Anna",
"lastName": "Smith"
}, {
"firstName": "Peter",
"lastName": "Jones"
}]
};{
"maps": [{
"id": "apple",
"iscategorical": "0"
}, {
"id": "ball",
"iscategorical": "0"
}],
"mask": {
"id1": "aaaaa"
},
"mask": {
"id1": "bbb"
},
"mask": {
"id1": "cccccc"
},
"om_points": "value",
"parameters": {
"id": "valore"
}
}"""
splitdata = data.split(';')
datatop = json.loads(splitdata[0])
databottom = json.loads(splitdata[1])
Then you can access required fields as follows
print(datatop['employees'][0]['firstName'])
print(datatop['employees'][0]['lastName'])
print(databottom['mask']['id1'])
print(databottom['maps'][0]['id'])

Categories

Resources