Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to delete a json row if all row is zero?

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    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
              }
            ]
          }
          },

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading