Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
Searching a Json or XML structure for a specific key / value pair in Python

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(lambda x,y:cmp(len(x),len(y)))
  else:
    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"
    sys.stdout.write("%16s%s"%(found[k],sep))


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 articles
Y117 - Python - Already written modules
  [4708] Scons - a build system in Python - building hello world - (2016-10-29)
  [4697] Month, Day, Year number to day of week and month names in Python - English and Swedish - (2016-06-23)
  [4696] Programming with random numbers - yet re-using the same values for testing - (2016-06-22)
  [4452] Binary data handling - Python and Perl - (2015-03-09)
  [4441] Reading command line parameters in Python - (2015-02-23)
  [4086] Cacheing class for Python - using a local SQLite database as a key/value store - (2013-05-14)
  [4085] JSON from Python - first principles, easy example - (2013-05-13)
  [3479] Practical Extraction and Reporting - using Python and Extreme Programming - (2011-10-14)
  [3465] How can I do an FTP transfer in Python? - (2011-10-05)
  [3442] A demonstration of how many Python facilities work together - (2011-09-16)
  [2931] Syncronise - software, trains, and buses. Please! - (2010-08-22)
  [2890] Dates and times in Python - (2010-07-27)
  [2506] Good example of recursion in Python - analyse an RSS feed - (2009-11-18)
  [2020] Learning Python - many new example programs - (2009-01-31)

Y151 - Python & XML
  [4594] XML handling in Python - a new teaching example using etree - (2015-12-09)
  [3082] XML handling in Python - SAX, DOM and XSLT examples - (2010-12-09)
  [2555] Bookkeeping - (2009-12-29)
  [2378] Handling XML in Perl - introduction and early examples - (2009-08-27)


Back to
Some gems from Intermediate Python
Previous and next
or
Horse's mouth home
Forward to
Convering from Python 2 to Python 3 - an update, and the 2to3 utility
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 3
Convering from Python 2 to Python 3 - an update, and the 2to3 utility
Searching a Json or XML structure for a specific key / value pair in Python
Some gems from Intermediate Python
Some gems from an introduction to Python
Melksham trial train service is to be made permanent
Course dates - from October 2016 to December 2017
4750 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2017: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/4710_Sea ... ython.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat