{
"id": 6,
"issuer_id": 1,
"total_points": 100,
"membership_details": {
"id": 367,
"valid_from": "2022-07-26T15:23:43.000 05:30",
"valid_through": "2023-07-26T15:23:43.000 05:30"
},
"specific_wallet_transactions": [
{
"id": 9,
"transaction_type": "Earn",
"points": 100,
"points_validity": "2022-04-23T00:00:00.000 05:30",
"order_meta_data": {},
"reason_code": "Earn",
"created_at": "2021-04-23T10:12:41.000 05:30",
"transaction_name": "Earn"
},
{
"id": 16,
"transaction_type": "Burn",
"points": 5,
"points_validity": "null",
"order_meta_data": {
"order_id": "12",
"given_wallet_ids": [
6
],
"used_wallet_ids": [
6
],
"burn_type": "pool burn"
},
"reason_code": "Burn",
"created_at": "2021-04-29T16:28:11.000 05:30",
"transaction_name": "Burn"
},
{
"id": 2775,
"transaction_type": "Burn",
"points": 100,
"points_validity": "null",
"order_meta_data": {},
"reason_code": "Expired Points Transaction",
"created_at": "2022-05-10T17:02:04.000 05:30",
"transaction_name": "Expire"
},
{
"id": 3654,
"transaction_type": "Earn",
"points": 100,
"points_validity": "2024-07-25T00:00:00.000 05:30",
"order_meta_data": {},
"reason_code": "Earn",
"created_at": "2022-07-26T15:23:42.000 05:30",
"transaction_name": "Earn"
}
],
"customer_id": 9,
"customer_email": "",
"issuer_name": "The qq",
"default_currency": "INR",
"conversion_rate": 1.0,
"points_monetary_value": 100.0,
"user_name": "null",
"segment_code": "ww"
}
This is my json and here i need last_transaction = o["specific_wallet_transactions"][-1] and i got it too...... now i need total_points also in last_transaction
how to achieve this just i need to append this total_points to last_transaction where last_transaction is dict
. I can pass as new param but, how to achieve with same variable now. thanks in advance .
CodePudding user response:
I am not sure whether you want to add the total_points
into the last_transaction['points']
or you want to add another "field" called total_points
to the last_transaction
. This code does both -
if isinstance(json_obj['specific_wallet_transactions'][-1], dict):
json_obj['specific_wallet_transactions'][-1]['points'] = json_obj['total_points']
json_obj['specific_wallet_transactions'][-1]['total_points'] = json_obj['total_points']
CodePudding user response:
I'm presuming that you want to sum the points
values contained in the specific_wallet_transactions
list.
# Check 'specific_wallet_transactions' is a list
if isinstance(o['specific_wallet_transactions'], list):
# Get the last transaction
last_transaction = o['specific_wallet_transactions'][-1]
# Check 'last_transaction' is a dictionary
if isinstance(last_transaction, dict):
# Calculate 'total_points' by calculating the sum of 'points' values
last_transaction['total_points'] = sum([t['points'] for t in o['specific_wallet_transactions']
if t['points'] is not None])
Complete code:
o = {
"id": 6,
"issuer_id": 1,
"total_points": 100,
"membership_details": {
"id": 367,
"valid_from": "2022-07-26T15:23:43.000 05:30",
"valid_through": "2023-07-26T15:23:43.000 05:30"
},
"specific_wallet_transactions": [
{
"id": 9,
"transaction_type": "Earn",
"points": 100,
"points_validity": "2022-04-23T00:00:00.000 05:30",
"order_meta_data": {},
"reason_code": "Earn",
"created_at": "2021-04-23T10:12:41.000 05:30",
"transaction_name": "Earn"
},
{
"id": 16,
"transaction_type": "Burn",
"points": 5,
"points_validity": "null",
"order_meta_data": {
"order_id": "12",
"given_wallet_ids": [
6
],
"used_wallet_ids": [
6
],
"burn_type": "pool burn"
},
"reason_code": "Burn",
"created_at": "2021-04-29T16:28:11.000 05:30",
"transaction_name": "Burn"
},
{
"id": 2775,
"transaction_type": "Burn",
"points": 100,
"points_validity": "null",
"order_meta_data": {},
"reason_code": "Expired Points Transaction",
"created_at": "2022-05-10T17:02:04.000 05:30",
"transaction_name": "Expire"
},
{
"id": 3654,
"transaction_type": "Earn",
"points": 100,
"points_validity": "2024-07-25T00:00:00.000 05:30",
"order_meta_data": {},
"reason_code": "Earn",
"created_at": "2022-07-26T15:23:42.000 05:30",
"transaction_name": "Earn"
}
],
"customer_id": 9,
"customer_email": "",
"issuer_name": "The qq",
"default_currency": "INR",
"conversion_rate": 1.0,
"points_monetary_value": 100.0,
"user_name": "null",
"segment_code": "ww"
}
if isinstance(o['specific_wallet_transactions'], list):
last_transaction = o['specific_wallet_transactions'][-1]
if isinstance(last_transaction, dict):
last_transaction['total_points'] = sum([t['points'] for t in o['specific_wallet_transactions']
if t['points'] is not None])
To view the result:
import json
print(json.dumps(last_transaction, indent=2))
Output:
{
"id": 3654,
"transaction_type": "Earn",
"points": 100,
"points_validity": "2024-07-25T00:00:00.000 05:30",
"order_meta_data": {},
"reason_code": "Earn",
"created_at": "2022-07-26T15:23:42.000 05:30",
"transaction_name": "Earn",
"total_points": 305
}