I am new to python. I want to extract the elements of array 'Address' from below json. I am trying to use map to split the elements using
r=<below json>
s=r["Customer"]
y=s.map(lambda x:x.split(","))
But I am getting the error as .....AttributeError: 'str' object has no attribute 'map'
Can you please advise which is the best way to do this.
{ "id": "981",
"Customer":
[
{
"Name": "abc",
"Title": "Mr",
"Gender": "M",
"Address": [
{
"Postcode": "2GF",
"StreetName": "Black Street",
"FlatNo": "123",
}
]
},
{
"Name": "xyz",
"Title": "Mrs",
"Gender": "F",
"Address": [
{
"Postcode": "2CX",
"StreetName": "White Street",
"FlatNo": "123",
}
]
}
]
}
Why not just:
data = { id: 981,
'Customer':
[
{
"Name": "abc",
"Title": "Mr",
"Gender": "M",
"Address": [
{
"Postcode": "2GF",
"StreetName": "Black Street",
"FlatNo": "123",
}
]
},
{
"Name": "xyz",
"Title": "Mrs",
"Gender": "F",
"Address": [
{
"Postcode": "2CX",
"StreetName": "White Street",
"FlatNo": "123",
}
]
}
]
}
for item in data['Customer']:
for data_item in item['Address']:
print(data_item['Postcode'], data_item['StreetName'], data_item['FlatNo'])
OUTPUT:
2GF Black Street 123
2CX White Street 123
"I want to extract the elements of array 'Address' from below json":
[x for dd in r['Customer'] for x in dd['Address']]
Related
my data looks like this:
[
{
"id": "00f0bbe514dcaf262c8a",
"status": "CL",
"type": "opportunity",
"locations": [
{
"name": "New York, USA",
"lat": 99.0853,
"lng": 99.7818,
"id": "456",
"type": "CI"
},
{
"name": "Boston, USA",
"lat": 80.0853,
"lng": 80.7818,
"id": "555",
"type": "CI"
},
{
"name": "London, UK",
"lat": 10.0853,
"lng": 10.7818,
"id": "999",
"type": "CI"
}
]
},
{
"id": "sadl9asod01",
"status": "CL",
"type": "opportunity",
"locations": [
{
"name": "Boston, USA",
"lat": 80.0853,
"lng": 80.7818,
"id": "555",
"type": "CI"
},
]
},
{
"id": "13094ulk",
"status": "CL",
"type": "project", # has right location but not type
"locations": [
{
"name": "Boston, USA",
"lat": 80.0853,
"lng": 80.7818,
"id": "555",
"type": "CI"
},
]
}
]
I want to build a query that the type must be opportunity:
type_q = ElasticQ('bool', must=[ElasticQ('match', type='opportunity')])
query = self.index.search().query(type_q)
I know how to build an "in" query with the dsl, for example:
excluded_ids = self._excluded_jobs() # list
query = query.exclude('terms', id=excluded_ids)
but, how can I add to the query what in SQL I would do like this:
WHERE type='opportunity'
AND
location.id in (1, 2, 3)
location represents here object inside the locations array of the doc
Something like:
type_q = ElasticQ('bool', must=[
ElasticQ('match', type='opportunity'),
ElasticQ('terms', id=excluded_ids),
])
Or, if you actually wanted to exclude those IDs:
type_q = ElasticQ('bool',
must=[ElasticQ('match', type='opportunity')]
must_not=[ElasticQ('terms', id=excluded_ids)]
)
This is an extension from a previous question that is unresolved but another way of looking at the problem HERE
{
"name": "Sarah",
"region": "south west",
"age": 21,
"occupation": "teacher",
"height": 145,
"education": "university",
"favouriteMovie": "matrix",
"gender": "female",
"country": "US",
"level": "medium",
"tags": [],
"data": "abc",
"moreData": "xyz",
"logging" : {
"systemLogging" : [ {
"environment" : "test",
"example" : [ "this", "is", "an", "example", "array" ]
} ]
}
}
If I wanted to see if a value exists for simple key value pairs in a python dictionary I could use the jmespath query to do so
"occupation == `teacher`"
>> True
"occupation == `dancer`"
>> False
But with more complicated data e.g nested dictionaries with lists/arrays the best I can do is filter to obtain the value
"logging.systemLogging[?environment == `test`] | [0].enabled"
>> test
"logging.systemLogging[?environment == `prod`] | [0].enabled"
>> None
How do I get the example above to return True or False rather then the value if it exists and None or empty array [] if it doesn't
Well, you can perfectly do the same:
logging.systemLogging[?environment == `test`] | [0].enabled != null
On
{
"name": "Sarah",
"region": "south west",
"age": 21,
"occupation": "teacher",
"height": 145,
"education": "university",
"favouriteMovie": "matrix",
"gender": "female",
"country": "US",
"level": "medium",
"tags": [],
"data": "abc",
"moreData": "xyz",
"logging": {
"systemLogging": [{
"environment": "test",
"example": ["this", "is", "an", "example", "array"]
}]
}
}
Will result in
false
While the same on this JSON — where logging.systemLogging[0].enabled: false was added:
{
"name": "Sarah",
"region": "south west",
"age": 21,
"occupation": "teacher",
"height": 145,
"education": "university",
"favouriteMovie": "matrix",
"gender": "female",
"country": "US",
"level": "medium",
"tags": [],
"data": "abc",
"moreData": "xyz",
"logging": {
"systemLogging": [{
"enabled": false,
"environment": "test",
"example": ["this", "is", "an", "example", "array"]
}]
}
}
Will result in
true
I have 2 separate JSON-Lists (with dicts) in it.
My goal is, that I want to iterate over list2 "currentUser", grab the values, search those values in list1, and as output print the value of "firstName"
e.g.
liste2: "currentUser": 123,
liste1: "id": "123", --> "firstName": "Lisa",
list1 = {
"X-API-KEY": "XyZzahZaksksXXXYYYOOO000",
"user": {
"email": "Lisa#BLA.com",
"firstName": "Lisa",
"id": "123",
},
"Flat": {
"city": "Munich",
"country": "2",
"countryCode": "DEU",
"currency": "EUR",
"date": "1587671397",
"flatmates": [
{
"email": "Lisa#BLA.com",
"firstName": "Lisa",
"id": "123",
},
{
"email": "Max#BLA.com",
"firstName": "Max",
"id": "124",
},
{
"email": "Hannah#BLA.com",
"firstName": "Hannah",
"id": "125",
},
{
"email": "Kai#BLA.com",
"firstName": "Kai",
"id": "126",
}
],
"founderId": "123",
"id": "99999",
"image": "",
"name": "ABC",
"postCode": "000000",
}
}
list2 = [
{
"creationDate": 1587671663,
"currentUser": 123,
"id": 1717134,
"title": "Do this",
"users": [
124,
126
]
},
{
"creationDate": 1587671663,
"currentUser": 126,
"id": 1717134,
"title": "Do that",
"users": [
123,
125
]
},
{
"creationDate": 1587671821,
"currentUser": 124,
"id": 1717134,
"title": "Clean this",
"users": [
125,
122
]
},
{
"creationDate": 1587671801,
"currentUser": 123,
"id": 1717134,
"title": "Clean that",
"users": [
124,
126
]
}
]
I am pretty new to python.
There are several mind-issues for me since there is a mix between lists and dictionaries in it and how to match/search for values for 2 separate lists/dicts
What I got so far: Iterate over the "CurrentUser"
for user in liste2:
print(user["currentUser"])
Has anyone some approaches?
In pure python with no other modules.
for user in list2:
for mate in list1['Flat']['flatmates']:
if user['currentUser'] == int(mate['id']):
# You found the person now execute this code...
One thing to note that in your list1 you flatmates id is not a integer but a string. So you have to convert that to an int in order to compare the two.
Hi I have two dictionaries 1.Primary, 2. Secondary
Need to check first field of both dictionary
If filed is same compare the title with primary and secondary
if Primary have missing dictionary which is in dictionary add that to primary
Primary dictionary
{"Latest":[
{
"name": "Employee",
"field": "employee",
"values": [
{
"title": "A",
"paragraph": "null",
"role": "Deveoper"
},
{
"title": "C",
"paragraph": "null",
"role": "Tester"
}
]
},
{
"name": "Project",
"field": "project",
"values": [
{
"title": "NEW_York",
"paragraph": "null",
"role": "Long Term"
}
]
},
{
"name": "Designation",
"field": "designation",
"values": [
{
"title": "Developer",
"paragraph": "null",
"role": "null"
}
]
}
]}
Secondary dictionary
[
{
"name": "Employee",
"field": "employee",
"values": [
{
"title": "A",
"paragraph": "null",
"role": "null"
},
{
"title": "B",
"paragraph": "null",
"role": "null"
}
]
},
{
"name": "Project",
"field": "project",
"values": [
{
"title": "NEW_York",
"paragraph": "test",
"role": "null"
}
]
},
{
"name": "Designation",
"field": "designation",
"values": [
{
"title": "Tester",
"paragraph": "null",
"role": "null"
}
]
}
]
Expected out
{"Latest":[
{
"name": "Employee",
"field": "employee",
"values": [
{
"title": "A",
"paragraph": "null",
"role": "Deveoper"
},
{
"title": "C",
"paragraph": "null",
"role": "Tester"
},
{
"title": "B",
"paragraph": "null",
"role": "null"
}
]
},
{
"name": "Project",
"field": "project",
"values": [
{
"title": "NEW_York",
"paragraph": "null",
"role": "Long Term"
}
]
},
{
"name": "Designation",
"field": "designation",
"values": [
{
"title": "Developer",
"paragraph": "null",
"role": "null"
},
{
"title": "Tester",
"paragraph": "null"
"role": "null"
}
]
}
]}
COde
for i in primary['Latest']:
for j in secondary:
if i['field'] == j['field']:
for a in i['values']:
for b in j['values']:
if a['title'] == b['title']:
i['values'].append(b)
My code executes indefinitely
The issue is that you are appending in i['values'] while iterating on it, creating unexpected behaviour. You can fix it by iterating on a "frozen" list if i['values'] like this:
from copy import deepcopy
for i in primary['Latest']:
for j in secondary:
if i['field'] == j['field']:
values = deepcopy(i['values'])
for a in values:
for b in j['values']:
if a['title'] == b['title']:
i['values'].append(b)
This resolves the "indefinitely" part, but doesn't quite work, here is the output:
print(json.dumps(primary, indent=2))
{
"Latest": [
{
"name": "Employee",
"field": "employee",
"values": [
{
"title": "A",
"paragraph": null,
"role": "Deveoper"
},
{
"title": "C",
"paragraph": null,
"role": "Tester"
},
{
"title": "A",
"paragraph": null,
"role": "null"
}
]
},
{
"name": "Project",
"field": "project",
"values": [
{
"title": "NEW_York",
"paragraph": "null",
"role": "Long Term"
},
{
"title": "NEW_York",
"paragraph": "test",
"role": "null"
}
]
},
{
"name": "Designation",
"field": "designation",
"values": [
{
"title": "Developer",
"paragraph": null,
"role": "null"
}
]
}
]
}
So I fixed it a little:
for primary_elem in primary['Latest']:
primary_titles = [value['title'] for value in primary_elem['values']]
for secondary_elem in secondary:
if secondary_elem['field'] == primary_elem['field']:
for secondary_value in secondary_elem['values']:
if secondary_value['title'] not in primary_titles:
primary_elem['values'].append(secondary_value)
Which gives
>>> print(json.dumps(primary, indent=2))
{
"Latest": [
{
"name": "Employee",
"field": "employee",
"values": [
{
"title": "A",
"paragraph": null,
"role": "Deveoper"
},
{
"title": "C",
"paragraph": null,
"role": "Tester"
},
{
"title": "B",
"paragraph": null,
"role": "null"
}
]
},
{
"name": "Project",
"field": "project",
"values": [
{
"title": "NEW_York",
"paragraph": "null",
"role": "Long Term"
}
]
},
{
"name": "Designation",
"field": "designation",
"values": [
{
"title": "Developer",
"paragraph": null,
"role": "null"
},
{
"title": "Tester",
"paragraph": null,
"role": "null"
}
]
}
]
}
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'])