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",
}
]
Related
I have two lists in Python something similar.
list1 = [
{"name": "sample1",
"place": "sampleplace1",
"value": "",
"time": "sampletime"
},
{"name": "sample2",
"place": "sampleplace2",
"value": "",
"time": "sampletime2"
}
]
list2 = [
{"name": "sample1",
"value": "10"
},
{"name": "sample2",
"value": "20"
}
]
I need to compare both the lists and whereever the name is matching, I need to update the value property in list1. I did by running a for loop on list1, get the matching list object from list2 for each list1 object and update the value.
I'm just wondering, is there a way to do this without running a for loop (something like Linq in C#)?
Sadly, Python does not have the same abilities as LINQ.
If you don't want to explicitly use a function there is map, but it uses a loop under the hood, as LINQ does.
You need for loops, like in :
list1 = [
{"name": "sample1",
"place": "sampleplace1",
"value": "",
"time": "sampletime"
},
{"name": "sample2",
"place": "sampleplace2",
"value": "",
"time": "sampletime2"
}
]
list2 = [
{"name": "sample1",
"value": "10"
},
{"name": "sample2",
"value": "20"
}
]
for elem1 in list1:
for elem2 in list2:
if elem1["name"] == elem2["name"]:
# match ! we replace the value
elem1["value"] = elem2["value"]
break # and stop searching
else:
print(f"no match in list2 for {elem1['name']=}")
# just for displaying the result
import json
print(json.dumps(list1, indent=2))
[
{
"name": "sample1",
"place": "sampleplace1",
"value": "10",
"time": "sampletime"
},
{
"name": "sample2",
"place": "sampleplace2",
"value": "20",
"time": "sampletime2"
}
]
I was wondering how to perform the following:
1.search for strings in a json and extract their nested components.
given:
"type": "blah",
"animals": [
{
"type": "dog1",
"name": "oscar",
}
},
{
"type": "dog2",
"name": "John",
}
},
{
"type": "cat1",
"name": "Fred",
"Colors": [
"Red"
],
"Contact_info": [
{
"Owner": "Jill",
"Owner_number": "123"
}
],
},
{
"type": "cat3",
"name": "Freddy",
"Colors": [
"Blue"
],
"Contact_info": [
{
"Owner": "Ann",
"Owner_number": "1323"
}
],
From this json, I would like to extract all of the animals that are of type cat like cat1 and cat2, as well as all of the information within that block. Like if I search for cat it should return:
{
"type": "cat1",
"name": "Fred",
"Colors": [
"Red"
],
"Contact_info": [
{
"Owner": "Jill",
"Owner_number": "123"
}
],
},
{
"type": "cat3",
"name": "Freddy",
"Colors": [
"Blue"
],
"Contact_info": [
{
"Owner": "Ann",
"Owner_number": "1323"
}
],
Not necessarily that format, but just all of the information that has type cat. Im trying to search for objects in a json file and extract features from that search as well as anything nested inside of it.
Here is my code so far:
f = open('data.json')
# returns JSON object as
# a dictionary
data = json.load(f)
# Iterating through the json
# list
for i in data:
if i['type'] == 'cat':
print(i['name'])
print(i['colors'])
break
# Closing file
f.close()```
To begin with, I recommend using the with statement that creates a runtime context that allows you to run a group of statements under the control of a context manager.
It’s much more readable and allows you to skip closing the file since the context manager will do everything for you.
Moving to your problem
Suppose your file is called animals.json
# Import json library to work with json files
import json
# Use context manager
with open("animals.json", "rb") as f:
# Load animals list from json file
animals = json.load(f)["animals"]
# Create a list of dictionaries if animal type contains "cat"
cats = [animal for animal in animals if "cat" in animal.get("type")]
# Write data to cats.json
json.dump(cats, open("cats.json", "w"), indent=4, sort_keys=False, ensure_ascii=False)
This code outputs the formatted cats.json file with all necessary elements:
[
{
"type": "cat1",
"name": "Fred",
"Colors": [
"Red"
],
"Contact_info": [
{
"Owner": "Jill",
"Owner_number": "123"
}
]
},
{
"type": "cat3",
"name": "Freddy",
"Colors": [
"Blue"
],
"Contact_info": [
{
"Owner": "Ann",
"Owner_number": "1323"
}
]
}
]
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))
I've got a JSON structure that looks like the following
{
"PersonInformation": {
"PhysicalStatus": "",
"OpenDetainers": [],
"StartDate": "",
"FacilityLog": [],
"CustStatus": "",
"EndDate": ""
},
"IdentityList": [
{
"CreationDate": "01/01/1999",
"PersonNames": [
{
"Suffix": "",
"FirstName": "Johnny",
"LastName": "Appleseed",
"MiddleName": ""
},
{
"Suffix": "",
"FirstName": "Foo",
"LastName": "Bar",
"MiddleName": ""
}
],
"PlaceOfBirthList": [
{
"City": "Boston",
"State": "MA",
"CountryCode": ""
}
]
}
]
}
I can parse the outer array like so, but I'm having trouble figuring out how to loop through one of the child arrays, like "PersonNames"
So I can do this
myjson = json.loads(json_data)
print myjson['PersonInformation']['PhysicalStatus']
for identity_list in myjson['IdentityList']:
print identity_list['CreationDate']
Which returns
OK
01/01/1999
as expected, but I don't know how to take it to the next level to traverse into and loop through "PersonNames"
Thanks for the assistance
You can iterate through the sub-list under the PersonNames key like this:
for identity in myjson['IdentityList']:
for person in identity['PersonNames']:
print person['FirstName'], person['LastName']
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'])