Related
dict1 and dict2 needs to be merged to form the final dict which will have the status with each process_id. I have also sent the order of status according to their priority.
the answer is in the merged_dict in the last.
master_id_process_list = [
{
"process_id": 100,
"key": "ssn"
},
{
"process_id": 101,
"key": "ssn"
}]
process_id_status_list = [
{
"process_id": 101,
"parent_process_id": 100,
"status": "PASSED"
}]
process_status = {
"PASSED": 30,
"NEEDS_MANUAL_REVIEW": 20,
"IN_PROGRESS": 10,
"FAILED": 0
}
merged_dict = [
{
"process_id": 100,
"key": "ssn",
"status": "PASSED"
},
{
"process_id": 101,
"key": "ssn",
"status": "PASSED"
}]
I got a resultant json from an API in the following format
[{
"Uid": "40cc6103-1cf0-4735-b882-d14d32018e58",
"Id": "9e1a0057-4570-4a6e-8ff5-88b2facbaf4e",
"Details": {
"Name": "Kiran"
}
}, {
"Uid": "40cc6103-1cf0-4735-b882-d14d32018e58",
"Id": "9e1a0057-4570-4a6e-8ff5-88b2facbaf4e",
"Details": {
"Age": "24"
}
},
{
"Uid": "196f5865-e9fe-4847-86ae-97d0bf57b816",
"Id": "84909ecb-c92e-48a7-bcaa-d478bf3a9220",
"Details": {
"Name": "Shreyas"
}
}
]
since the Uid and Id are same for multiple entires, can I club them togeather with Details key being the comma seperate key,value pair? Something like mentioned below
[{
"Uid": "40cc6103-1cf0-4735-b882-d14d32018e58",
"Id": "9e1a0057-4570-4a6e-8ff5-88b2facbaf4e",
"Details": {
"Name": "Kiran",
"Age": "24"
}
},
{
"Uid": "196f5865-e9fe-4847-86ae-97d0bf57b816",
"Id": "84909ecb-c92e-48a7-bcaa-d478bf3a9220",
"Details": {
"Name": "Shreyas"
}
}]
Please Guide me on this for the approach to be followed. Thanks
What you need is the dictionary function update(). Here's an example:
A = [{
"Uid": "40cc6103-1cf0-4735-b882-d14d32018e58",
"Id": "9e1a0057-4570-4a6e-8ff5-88b2facbaf4e",
"Details": {
"Name": "Kiran"
}
}, {
"Uid": "40cc6103-1cf0-4735-b882-d14d32018e58",
"Id": "9e1a0057-4570-4a6e-8ff5-88b2facbaf4e",
"Details": {
"Age": "24"
}
},
{
"Uid": "196f5865-e9fe-4847-86ae-97d0bf57b816",
"Id": "84909ecb-c92e-48a7-bcaa-d478bf3a9220",
"Details": {
"Name": "Shreyas"
}
}
]
B = []
def find(uid, id_):
for i, d in enumerate(B):
if d['Uid'] == uid and d['Id'] == id_:
return i
return -1
for d in A:
if (i := find(d['Uid'], d['Id'])) < 0:
B.append(d)
else:
B[i]['Details'].update(d['Details'])
print(B)
Prettyfied output:
[
{
"Uid": "40cc6103-1cf0-4735-b882-d14d32018e58",
"Id": "9e1a0057-4570-4a6e-8ff5-88b2facbaf4e",
"Details": {
"Name": "Kiran",
"Age": "24"
}
},
{
"Uid": "196f5865-e9fe-4847-86ae-97d0bf57b816",
"Id": "84909ecb-c92e-48a7-bcaa-d478bf3a9220",
"Details": {
"Name": "Shreyas"
}
}
]
Note:
This could be very inefficient if your API response contains very large numbers of dictionaries. You might need a completely different approach
You should iterate over the list and merge with accumulator with (Uid, Id) as key:
from typing import Dict, List
l = [{
"Uid": "40cc6103-1cf0-4735-b882-d14d32018e58",
"Id": "9e1a0057-4570-4a6e-8ff5-88b2facbaf4e",
"Details": {
"Name": "Kiran"
}
}, {
"Uid": "40cc6103-1cf0-4735-b882-d14d32018e58",
"Id": "9e1a0057-4570-4a6e-8ff5-88b2facbaf4e",
"Details": {
"Age": "24"
}
},
{
"Uid": "196f5865-e9fe-4847-86ae-97d0bf57b816",
"Id": "84909ecb-c92e-48a7-bcaa-d478bf3a9220",
"Details": {
"Name": "Shreyas"
}
}
]
def mergeItem(it: Dict, acc: Dict) -> Dict:
uid = it["Uid"]
id = it["Id"]
if (uid, id) in acc:
acc[(uid, id)] = {"Uid": uid, "Id": id, "Details": {**acc[(uid, id)]["Details"], **it["Details"]}}
else:
acc[(uid, id)] = {"Uid": uid, "Id": id, "Details": it["Details"]}
return acc
def mergeList(a:List) -> Dict:
acc = {}
for v in a:
acc = mergeItem(v, acc)
return acc
print(list(mergeList(l).values()))
# [
# {
# 'Uid': '40cc6103-1cf0-4735-b882-d14d32018e58',
# 'Id': '9e1a0057-4570-4a6e-8ff5-88b2facbaf4e',
# 'Details': {'Name': 'Kiran', 'Age': '24'}},
# {
# 'Uid': '196f5865-e9fe-4847-86ae-97d0bf57b816',
# 'Id': '84909ecb-c92e-48a7-bcaa-d478bf3a9220',
# 'Details': {'Name': 'Shreyas'}
# }
# ]
I have a JSON which looks like this:
{
"data": [
{
"Name": "Hello",
"Number": "20"
},
{
"Name": "Beautiful",
"Number": "22"
},
{
"Name": "World",
"Number": "25"
},
{
"Name": "!",
"Number": "28"
}
}
and I want to get everything what is smaller than 28, it should look like this:
{
"data": [
{
"Name": "Hello",
"Number": "20"
},
{
"Name": "Beautiful",
"Number": "22"
},
{
"Name": "World",
"Number": "25"
}
}
I looked for a solution but all I have found was to remove an exact value.
I'm doing this with a much larger file this is just an example.
You can do it with a simple for loop
import json
with open('your_path_here.json', 'r') as f:
data = json.load(f)
for elem in data['data']:
if int(elem['Number']) >= 28:
data['data'].remove(elem)
print(data)
>>> {
"data": [
{
"Name": "Hello",
"Number": "20"
},
{
"Name": "Beautiful",
"Number": "22"
},
{
"Name": "World",
"Number": "25"
}
}
An example could use list comprehension:
data = {
"data": [
{
"Name": "Hello",
"Number": "20"
},
{
"Name": "Beautiful",
"Number": "22"
},
{
"Name": "World",
"Number": "25"
},
{
"Name": "!",
"Number": "28"
}
]
}
filter_ = 28
filtered = {
"data": [
item for item in data["data"]
if int(item["Number"]) < filter_
]
}
print(filtered)
Basically, this creates iterates through data["data"], checks if that current item's number is less than the filter (28 in this case), and adds those to the list. You're left with:
{'data': [{'Name': 'Hello', 'Number': '20'}, {'Name': 'Beautiful', 'Number': '22'}, {'Name': 'World', 'Number': '25'}]}
...which should be what you need, but unformatted.
However, for larger JSON files, you might want to look into ijson, which allows you to load json files in a memory-efficient way. Here's an example:
import ijson
import json
filter_ = 28
with open('data.json', 'r') as file:
items = ijson.items(file, 'data.item')
filtered = [item for item in items if int(item["Number"]) < filter_]
with open('filtered.json', 'w') as output:
json.dump(filtered, output, indent=2)
Try this code online
I have two JSONs. One has entries like this:
one.json
"data": [
{
"results": {
"counties": {
"32": 0,
"96": 0,
"12": 0
},
"cities": {
"total": 32,
"reporting": 0
}
},
"element_id": 999
},
The other has entries like this:
two.json
"data": [
{
"year": 2020,
"state": "Virginia",
"entries": [
{
"first_name": "Robert",
"last_name": "Smith",
"entry_id": 15723,
"pivot": {
"county_id": 32,
"element_id": 999
}
},
{
"first_name": "Benjamin",
"last_name": "Carter",
"entry_id": 15724,
"pivot": {
"county_id": 34,
"element_id": 999
}
}
],
"element_id": 999,
},
I want to join one.json to two.json based on element_id. The JSONs have lots of element_ids so there is an element of finding the right one to append to. Is there a way to use append to do this based on element_id without having to use a for loop? An appended version of the second JSON above would look like this:
joined.json
"data": [
{
"year": 2020,
"state": "Washington",
"entries": [
{
"first_name": "Robert",
"last_name": "Smith",
"entry_id": 15723,
"pivot": {
"county_id": 32,
"element_id": 999
}
},
{
"first_name": "Benjamin",
"last_name": "Carter",
"entry_id": 15724,
"pivot": {
"county_id": 34,
"element_id": 999
}
}
],
"element_id": 999,
{
"results": {
"counties": {
"32": 0,
"96": 0,
"12": 0
},
"cities": {
"total": 32,
"reporting": 0
}
},
},
What I have so far:
for item in one:
#this goes through one and saves each unique item in a couple variables
temp_id = item["element_id"]
temp_datachunk = item
#then I try to find those variables in two and if I find them, I append
for data in two:
if data["element_id"] == temp_id:
full_data = data.append(item)
print(full_data)
Right now, my attempt dies at the append. I get AttributeError: 'dict' object has no attribute 'append'.
Something like this should work:
source = '''
[{
"results": {
"counties": {
"32": 0,
"96": 0,
"12": 0
},
"cities": {
"total": 32,
"reporting": 0
}
},
"element_id": 999
}]
'''
target = """
[{
"year": 2020,
"state": "Virginia",
"entries": [{
"first_name": "Robert",
"last_name": "Smith",
"entry_id": 15723,
"pivot": {
"county_id": 32,
"element_id": 999
}
},
{
"first_name": "Benjamin",
"last_name": "Carter",
"entry_id": 15724,
"pivot": {
"county_id": 34,
"element_id": 999
}
}
],
"element_id": 999
}]
"""
source_j = json.loads(source)
target_j = json.loads(target)
jsonpath_expr = parse('$..element_id')
source_match = jsonpath_expr.find(source_j)
target_match = jsonpath_expr.find(target_j)
if source_match[0].value==target_match[0].value:
final = target_j+source_j
print(final)
The output is the combined json.
I am trying to create two seperate dictionaries, one that only holds the car information, and the second, which just holds the ticket information.
{
"cars": [{
"model": "toyota",
"plate": "A11",
"tickets": [{
"amount": 50,
"type": "A1"
},
{
"amount": 34,
"type": "A2"
}
]
},
{
"model": "mazda",
"plate": "A11",
"tickets": [{
"amount": 50,
"type": "A1"
},
{
"amount": 34,
"type": "A2"
}
]
}
]
}
import json
with open('jsonfile', 'r') as data:
cars_dict = json.load(data)
Then the loop to generate the two separate dicts. I created the loop, but still not achieving the result properly.
Desired output will be:
dict_cars = [{'Model':'Toyota', 'Plate':'A11'},
{'Model':'Mazda', 'Plate':'A13'}]
or
dict_cars = [{'Model':'Toyota', 'Plate':'A11', Tickets[......]},
{'Model':'Mazda', 'Plate':'A13'}, Tickets[......]]
dict_tickets = [{'amount:50',type:'A1'},
{'amount:34',type:'A2'},
{'amount:50',type:'A1'},
{'amount:34',type:'A2'}]
simply loop through the json object and parse the value:
d = {
"dict_cars": [{
"model": "toyota",
"plate": "A11",
"tickets": [{
"amount": 50,
"type": "A1"
},
{
"amount": 34,
"type": "A2"
}
]
},
{
"model": "mazda",
"plate": "A11",
"tickets": [{
"amount": 50,
"type": "A1"
},
{
"amount": 34,
"type": "A2"
}
]
}
]
}
dict_cars=[]
dict_tickets=[]
for i,j in d.items():
for k in j:
dict_cars.append({k.get('model',''):k.get("plate","")})
for t in k.get('tickets',""):
dict_tickets.append(t)
print("dict_cars = ",dict_cars)
print("dict_tickets = ",dict_tickets)
out
dict_cars = [{'toyota': 'A11'}, {'mazda': 'A11'}]
dict_tickets = [{'amount': 50, 'type': 'A1'}, {'amount': 34, 'type': 'A2'}, {'amount': 50, 'type': 'A1'}, {'amount': 34, 'type': 'A2'}]
import json
with open('file.json', 'r') as data:
text = data.read()
cars = json.loads(text)['cars']
tickets = []
for arr in cars:
for item in arr['tickets']:
tickets.append(item)
del arr['tickets']
print(tickets)
print(cars)
output:
[{u'amount': 50, u'type': u'A1'}, {u'amount': 34, u'type': u'A2'}, {u'amount': 50, u'type': u'A1'}, {u'amount': 34, u'type': u'A2'}]
[{u'plate': u'A11', u'model': u'toyota'}, {u'plate': u'A11', u'model': u'mazda'}]