Home > other >  is there a way to check nested dictionary values and if they have 0 or null or empty string then del
is there a way to check nested dictionary values and if they have 0 or null or empty string then del

Time:10-05

this is json file that i want to convert to python

{
    "UniqueId": "PO3589472",
    "FareType": 2,
    "BookedBy": "Api ",
    "OrderBy": "Api ",
    "ClientBalance": 0,
    "Error": null,
    "Success": true,
    "TktTimeLimit": "2022-08-10T14:11:45",
    "Category": 21,
    "Status": 21,
    "RefundMethod": 1,
    "TravelItinerary": {
        "ItineraryInfo": {
            "ItineraryPricing": {
                "BaseFare": 8469250,
                "ServiceTax": 0,
                "TotalTax": 993000,
                "TotalFare": 9462250,
                "TotalCommission": 0,
                "Currency": "IRR"
            },
            "CustomerInfoes": [
                {
                    "Customer": {
                        "Gender": 0,
                        "PassengerType": 1,
                        "PassportNumber": "",
                        "NationalId": "1829961233",
                        "Nationality": "IR",
                        "DateOfBirth": "1996-07-08T00:00:00",
                        "PassportExpireDate": "0001-01-01T00:00:00",
                        "PassportIssueCountry": "IR",
                        "PassportIssueDate": "2022-08-10T00:00:00",
                        "PaxName": {
                            "PassengerFirstName": "MAJID",
                            "PassengerMiddleName": null,
                            "PassengerLastName": "MAJIDIFAR",
                            "PassengerTitle": 0
                        }
                    },
                    "ETickets": "8151405444745",
                    "ETicketNumbers": [
                        {
                            "ETicketNumber": "8151405444745",
                            "EticketStatus": 1,
                            "IsRefunded": false,
                            "DateOfIssue": "2022-08-10T13:58:47",
                            "AirlinePnr": "TXNXM",
                            "TotalRefund": 0
                        }
                    ]
                }
            ],
            "ReservationItems": [
                {
                    "AirEquipmentType": "737",
                    "AirlinePnr": "TXNXM",
                    "ArrivalAirportLocationCode": "ABD",
                    "ArrivalDateTime": "2022-08-17T23:25:00",
                    "ArrivalTerminal": "",
                    "Baggage": "20KG",
                    "DepartureAirportLocationCode": "THR",
                    "DepartureDateTime": "2022-08-17T22:05:00",
                    "DepartureTerminal": "Terminal 4",
                    "FlightNumber": "3750",
                    "JourneyDuration": "01:20",
                    "JourneyDurationPerMinute": 0,
                    "MarketingAirlineCode": "EP",
                    "OperatingAirlineCode": "EP",
                    "ResBookDesigCode": "Y",
                    "StopQuantity": 0,
                    "IsCharter": false,
                    "TechnicalStops": [],
                    "IsReturn": false,
                    "CabinClassCode": 1
                }
            ],
            "TripDetailPtcFareBreakdowns": [
                {
                    "PassengerTypeQuantity": {
                        "PassengerType": 1,
                        "Quantity": 1
                    },
                    "TripDetailPassengerFare": {
                        "BaseFare": 8469250,
                        "ServiceTax": 0,
                        "Tax": 993000,
                        "TotalFare": 9462250,
                        "Commission": 0,
                        "Currency": "IRR"
                    }
                }
            ],
            "PhoneNumber": "09359276735",
            "Email": "[email protected]",
            "ItineraryFareFamily": null
        },
        "BookingNotes": [],
        "Services": []
    },
    "ValidatingAirlineCode": "EP",
    "DirectionInd": 1,
    "OnlineCheckIn": false,
    "AirRemark": [],
    "curl_error": false
}

CodePudding user response:

This function will recursively iterate over dictionary and remove keys with empty values. However, there may be some issues with nested lists, I will try to finish it later.

def remove_keys_in_one_level(d):
    to_pop = []
    for key, value in d.items():
        if not value:  # configure this condition if you don't want to remove empty lists, dictionaries...
            to_pop.append(key)
    for i_to_pop in to_pop:
        d.pop(i_to_pop)

    for key, value in d.items():
        if isinstance(value, dict):
            d[key] = remove_keys_in_one_level(value)
        elif isinstance(value, list):
            for lst_indx, lst_item in enumerate(value):  # will be a problem with multiple nested lists...
                if isinstance(lst_item, dict):
                    value[lst_indx] = remove_keys_in_one_level(lst_item)

    return d

rsl = remove_keys_in_one_level(jsn)

CodePudding user response:

As mentioned already, this begs for recursion. Here is an example:

import json
from collections.abc import Callable, Hashable
from typing import Any


def filter_dict(
    dictionary: dict[Hashable, Any],
    exclude_func: Callable[[Any], bool],
) -> None:
    discard = set()
    for key, value in dictionary.items():
        if isinstance(value, dict):
            filter_dict(value, exclude_func)
        elif exclude_func(value):
            discard.add(key)
    for key in discard:
        del dictionary[key]


def is_nothing(value: Any) -> bool:
    return value is None or value == 0 or value == ""


def main() -> None:
    j = "{}"  # Your JSON string here
    d = json.loads(j)
    filter_dict(d, is_nothing)
    print(json.dumps(d, indent=4))


if __name__ == '__main__':
    main()

It does not handle JSON objects nested inside arrays (i.e. dictionaries nested inside lists), but I think you can build on that yourself.

  • Related