3

I have a JSON object in which I want to remove the null key with the value that is my JSON. Please give me a solution how can I remove the null attribute with key and value from the object and can get without null keys data

UPDATE:

if request.method == 'POST': 
    all_products = request.data['products']
    
    db = CategoriesActiveORM.get_connection()
    keywords = db.table('categories'). \
        select('products', db.raw('count(*) as total')). \
        where_not_null('products'). \
        group_by('products'). \
        order_by('total', 'desc'). \
        limit(4). \
        get()

    total = keywords.sum('total')
    industries = {}
    for key in keywords:
        industries[key['products']] = round(int(key['total']) / total * 100, 2)
    
    print('industries', industries)
    
    industries = industries.pop("null", None)   

    print('industries', industries)
    
    industries.pop("null", None)
    rint('industries', industries)

    return JsonResponse(industries)

Print 1:

{
    "null": 87.37,
    " Product1": 4.95,
    " Product2": 4.44,
    " Product3": 3.24
}

Print 2:

None

Print 3:

{
    "null": 87.37,
    " Product1": 4.95,
    " Product2": 4.44,
    " Product3": 3.24
}
Spiral
  • 917
  • 1
  • 9
  • 15
  • 3
    Just remove the key null. `dictionary.pop("null", None)` –  Mar 02 '22 at 15:03
  • Thanks @SembeiNorimaki for replying to me. That is not working giving null result after pop null – Spiral Mar 02 '22 at 15:14
  • show us the code where you create your object how you apply the pop and print the value of your object after that –  Mar 02 '22 at 15:15
  • Sorry. I have updated my question. Thanks – Spiral Mar 02 '22 at 15:25
  • Dear, @SembeiNorimaki I have updated my question. Know you can understand my problem. Sorry, i was creating confusion. Thanks – Spiral Mar 02 '22 at 17:01
  • ok, I don't see in your code where are you applying the `industries.pop("null", None)` suggested as solution –  Mar 02 '22 at 17:03
  • let me define in code – Spiral Mar 02 '22 at 17:04
  • Dear, @SembeiNorimaki I updated question with using ``pop`` methon – Spiral Mar 02 '22 at 17:06
  • yes, but you should not assign the result of pop into industries. also, if you print industries before removing null you will still see the key. you should print after. –  Mar 02 '22 at 17:07
  • Yes. I have printed after then giving ``None`` not giving any result after pop ``null`` – Spiral Mar 02 '22 at 17:11
  • of course, because you are doing `industries = industries.pop("null", None)` instead of the suggested solution which is `industries.pop("null", None)`. You have an answer giving you the exact code you should execute. –  Mar 02 '22 at 17:11
  • Yes, I checked also with ``industries.pop("null", None)`` then not removing ``null`` key – Spiral Mar 02 '22 at 17:15

1 Answers1

2

If you are looking to delete a key from the dict a similar question has been answered here: How can I remove a key from a Python dictionary?

You are assigning industries to the item you just popped from the dictionary.

Solution A

To fix this you must not assign the popped item to industries.

Just remove the industries = as follows:

Before

industries = industries.pop("null", None)

After

industries.pop("null", None)

This should give you the result you desire.

Solution B

If that didn't work try using del as follows:

del industries["null"]

EDIT

It seems like you forgot the quotations marks on the None key in the object, that seems to have solved your problem.

Liam
  • 461
  • 4
  • 27