With data loaded from JSon or XML structures, you'll often want to search for attributes by element name - without having to code the structure, and with the code adapting itself to mean subtle structure changes in the suppied data.
Here's an example written during lat week's tailored Intermediate Python course
... taking a Json object (loaded from our web resource) and displaying a list of all the elements of a specific name.
As good python code encapsulate all the hard work into methods, our main progam is very short.
If I want to find the value of all the fields keyed "where" ... Loading the data
fh = open("jflow.json")
igot = json.load(fh)
found = parseDict(igot,"where")
and the hard work is done in parseDict as json.load returns a dict. parseDict has been written to return a list.
Sorting the results:
if sys.version_info < (3,0):
found.sort(key = lambda x:len(x))
and displaying the results, 4 per line.
print("Here are the remote IP visitors to your images")
for k in range(len(found)):
sep = ((k+1)%4 and k+1 != len(found)) and " " or "\n"
Full source code is [here]
. The data ('pretty printed' as Json compressed is hard to read!) is [here]
parseDict is a piece of recursive code - two re-entrant pieces of code which call each other as they traverse your structure and find lists and dicts within other lists and dicts. I am not
normally a fan of recursive / re-entrant code on courses, as they're typically over-clever solutions looking for problems, but this particular use is sensible.
I'll cover this for you on Python Intermediate courses
or on Python Programming courses
(in that case after class hours if you make a specific request, as not everyone wants to do this). (written 2016-10-30)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articlesY117 - Python - Already written modules 
Scons - a build system in Python - building hello world - (2016-10-29) 
Month, Day, Year number to day of week and month names in Python - English and Swedish - (2016-06-23) 
Programming with random numbers - yet re-using the same values for testing - (2016-06-22) 
Binary data handling - Python and Perl - (2015-03-09) 
Reading command line parameters in Python - (2015-02-23) 
Cacheing class for Python - using a local SQLite database as a key/value store - (2013-05-14) 
JSON from Python - first principles, easy example - (2013-05-13) 
Practical Extraction and Reporting - using Python and Extreme Programming - (2011-10-14) 
How can I do an FTP transfer in Python? - (2011-10-05) 
A demonstration of how many Python facilities work together - (2011-09-16) 
Syncronise - software, trains, and buses. Please! - (2010-08-22) 
Dates and times in Python - (2010-07-27) 
Good example of recursion in Python - analyse an RSS feed - (2009-11-18) 
Learning Python - many new example programs - (2009-01-31)Y151 - Python & XML 
XML handling in Python - a new teaching example using etree - (2015-12-09) 
XML handling in Python - SAX, DOM and XSLT examples - (2010-12-09) 
Bookkeeping - (2009-12-29) 
Handling XML in Perl - introduction and early examples - (2009-08-27)
Some other Articles
The technical article feed continues - personal updates more proactive on Facebook now!Equality (in Python)A reminder of the key issues to consider in moving from Python 2 to Python 3Convering from Python 2 to Python 3 - an update, and the 2to3 utilitySearching a Json or XML structure for a specific key / value pair in PythonSome gems from Intermediate PythonSome gems from an introduction to PythonMelksham trial train service is to be made permanentCourse dates - from October 2016 to December 2017