title: Search a json key by closest match python
body: <p>I was trying to learn how I can search through json key pairs by its closest match. So I did some google searching and got to here: <a href="https://linuxhint.com/search_json_python/" rel="nofollow noreferrer">https://linuxhint.com/search_json_python/</a></p>
  <p>Eventually, I copied and pasted the example 3 code into my idle and it works but if I type anything other than an exact match (for example <code>Pe</code> instead of <code>Pen</code>) it will raise the exception.</p>
  <p>I essentially want to find a why to print the results of a search of anything that wasn't a match but was close. (kind of like when using os.walk)</p>
  <p>Here is the code:</p>
  <pre><code>#!/usr/bin/env python3
  
  # Import json module
  import json
  
  # Define json variable
  jsondata = &quot;&quot;&quot;[
   {
    &quot;name&quot;:&quot;Pen&quot;,
    &quot;unit_price&quot;:5
   },
   {
    &quot;name&quot;:&quot;Eraser&quot;,
    &quot;unit_price&quot;:3
   },
   {
    &quot;name&quot;:&quot;Pencil&quot;,
    &quot;unit_price&quot;:10
   },
   {
    &quot;name&quot;:&quot;White paper&quot;,
    &quot;unit_price&quot;:15
   }
  ]&quot;&quot;&quot;
  
  # load the json data
  items = json.loads(jsondata)
  
  # Input the item name that you want to search
  item = input(&quot;Enter an item name:\n&quot;)
  
  # Define a function to search the item
  def search_price (name):
   for keyval in items:
    if name.lower() == keyval['name'].lower():
     return keyval['unit_price']
  
  # Check the return value and print message
  if (search_price(item) != None):
    print(&quot;The price is:&quot;, search_price(item))
  else:
    print(&quot;Item is not found&quot;)
  
  </code></pre>
  <p>Any ideas or links would be much appreciated.</p>
  <p>Thank you for your time,</p>