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
}
12
  • 3
    Just remove the key null. dictionary.pop("null", None) Commented Mar 2, 2022 at 15:03
  • Thanks @SembeiNorimaki for replying to me. That is not working giving null result after pop null Commented Mar 2, 2022 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 Commented Mar 2, 2022 at 15:15
  • Sorry. I have updated my question. Thanks Commented Mar 2, 2022 at 15:25
  • Dear, @SembeiNorimaki I have updated my question. Know you can understand my problem. Sorry, i was creating confusion. Thanks Commented Mar 2, 2022 at 17:01

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

14 Comments

Thanks, bro for replying to me. but giving error KeyError: 'null'. I am getting this JSON object from function and want to remove that.
I checked in the separate compiler working fine but in my code which is coming to the same result. but not working
Can you share the code for keywords?
Okay let me define
Amazing Dear Brother. That is working Fine. Thanks a lot
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.