Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1075235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:14:39+00:00 2026-05-16T21:14:39+00:00

I have a json hash which has a lot of keys. I retrieve this

  • 0

I have a json hash which has a lot of keys. I retrieve this hash from a web service at regular intervals and for different parameters etc. This has more or less fixed structure, in the sense that keys are sometimes missing.
So I end up with a lot of code of the following nature

Edit:
Sample data

data =
{
id1 : {dict...},
id2 : {dict..},
'' : {value...},
...
}

for item in data:
   id = data.get("id")
   if not id:
      continue
   ...

I want to skip the 3rd element and move on. The structure data is a nested dict and I loop inside each of these nests. There are keys missing there as well 🙁

I was wondering if there was a more elegant solution than having 50 different ifs and continues

Thanks

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T21:14:40+00:00Added an answer on May 16, 2026 at 9:14 pm

    How about iterating over the dict keys and doing your processing:

    data = {
    'id1' : {'a':"", 'b':""},
    'id2' : {'c':"", 'd':""},
    '' : {'c':"", 'd':""},
    "": {'c':"", 'd':""},
    }
    
    for key in data.iterkeys():
        if key:
            print key
            print "Processing %s" % key
            # do further processing of data[key]
    

    This outputs the following. Notice that it skips processing for which key is missing.

    id2
    Processing id2
    id1
    Processing id1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a json form field which has a check box as coded :
I have used JSON::Any in my program to transfer the hash between client and
I have JSON like this: { something: http://something.com } and HTML like this: <a
I have JSON string that has nested objects with dynamic names that vary each
I have JSON text that looks like this: { ok: true, totalPages: 256, arReports:
i have json data like this {GetStudentDetails: {TotalCount:5, RootResults:[ {city:West Chester,country:USA,state:PA ,student_id:100}, {city:Philly,country:USA,state:PA,student_id:101}, {city:Buffalo,country:USA,state:NY,student_id:102},
I have a JSON service, where I use some photos. I want to make
We have an web application build using node js (express js), which is behind
How to convert a ruby hash object to JSON? So I am trying this
I have a json structure that I'm decoding that looks like this: person =>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.