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

  • SEARCH
  • Home
  • 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 9063567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:02:11+00:00 2026-06-16T16:02:11+00:00

I’m having trouble dynamically creating a Python dictionary path to loop through and validate

  • 0

I’m having trouble dynamically creating a Python dictionary path to loop through and validate a value. Here’s what I’d like to do:

Make API call using Requests 1.0 and store the JSON response in a dict.

response = requests.get(path/to/file.json).json()

The response object will be formatted as follows:

{   
"status": "OK",
"items": [
    {
        "name": "Name 1",
        "id": 0,
        "address":{
            "city": "New York",
        }
    },
    {
        "name": "Name 2",
        "id": 1,
        "address":{
            "city": "New York",
        }
    },
    {
        "name": "Name 3",
        "id": 2,
        "address":{
            "city": "New York",
        }
    }]
}

Send the response dict, field and value to a function for validation. The function would take the response object and append the field entry to it to define its path then validate against the value. So in theory it would be:

response[field] = value

The code that I wrote to do this was:

def dynamic_assertion(response, field, value):
        i = 0
        stations = "response['items']"
        count = len(response['items'])
        while i < count:
            path = '%s[%s]%s' % (stations, i, field)
            path = path.strip("")
            if path != value:
                print type(path)
                return False
            i += 1
        return True

dynamic_assertion(response, "['address']['city']", "New York")

I realize that once I create the path string it is no longer an object. How do I create this in a way that will allow me to keep the response object and append the reference path to traverse through? Is this even possible?!

  • 1 1 Answer
  • 0 Views
  • 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-06-16T16:02:13+00:00Added an answer on June 16, 2026 at 4:02 pm

    I think you’d be better off avoiding a single path string in favor of a tuple or list of strings which represent the individual keys in the nested dictionaries. That is, rather than "['address']['city']" being your field argument, you’d pass ("address", "city"). Then you just need a loop to go through the keys and see if the final value is the correct one:

    def dynamic_assertion(response, field, value):
        for item in response["items"]:
            for key in field:
                item = item[key] # go deeper into the nested dictionary
            if item != value:
                return False # raising an exception might be more Pythonic
        return True
    

    Example output (given the response dict from the question):

    >>> dynamic_assertion(response, ("address", "city"), "New York")
    True
    >>> dynamic_assertion(response, ("address", "city"), "Boston")
    False
    >>> response["items"][2]["address"]["city"] = "Boston" # make response invalid 
    >>> dynamic_assertion(response, ("address", "city"), "New York")
    False
    >>> dynamic_assertion(response, ("address", "city"), "Boston")
    False
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I am trying to loop through a bunch of documents I have to put
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

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.