Home > OS >  How to delete a json row if all row is zero?
How to delete a json row if all row is zero?

Time:03-30

I have complex JSON data and I am creating a table from this data. I can get all values clearly.

But I want to remove or hide a row if all data is zero. In my case, if my counter is 5 then I should remove 'title' and 'values'

I created a function for that but I cannot remove the line. How can I do it?

    for data1 in financial_balance:
        for data2,key in data1.items():
            if isinstance(key, str) != True:
                for k in key:
                    for l,d in k.items():
                        if isinstance(d, str) != True:
                            for x in d:
                                count1 = 0
                                counter = 0
                                for e in x["values"]:
                                    count1  = 1
                                    if e["value"] == 0:
                                        counter  = 1
                                
                                if counter == 5:
                                    # remove row

my data:

[
  {
    'name': 'Balance Sheet',
    'dataList': [
      {
        'mainEntry': 'Assets',
        'titles': [
      
          {
            'title': 'Trade',
            'values': [
              {
                'yea r': 2020,
                'value': 268057,
                'colorCode': None
              },
              {
                'year': 2019,
                'value': 421621,
                'colorCod e': None
              },
              {
                'year': 'Year over year trends 2019 vs 2020',
                'value': -0.36,
                'colorCode': None
              },
              {
                'year': 'Common Size Analysis 2020',
                'value': 0.12,
                'colorCode': None
              },
              {
                'year': 'Common Size Analysis 2019',
                'value': 0.14,
                'colorCode': None
              }
            ]
          },
          {
            'title': 'Inventories ',
            'values': [
              {
                'year': 2020,
                'value': 0,
                'colorCode': None
              },
              {
                'year': 2019,
                'value': 0,
                'colorCode': None
              },
              {
                'year': 'Year over year trends 2019 vs 2020',
                'value': 0,
                'colorCod e': None
              },
              {
                'year': 'Common Size Analysis 2020',
                'value': 0,
                'colorCode': None
              },
              {
                'year': 'Common Size Analysis 2019',
                'value': 0,
                'colorCode': None
              }
            ]
          }
          },

CodePudding user response:

You can use the del keyword to delete dictionary keys from a dictionary. For example, del x[e]["value"] .

  • Related