title: How To Create Generic JSON Parsing Code?
body: <p>So I'm parsing a list returned by parsing some json with <a href="https://github.com/talentdeficit/jsx" rel="nofollow" title="jsx">jsx</a>.  I'm pulling out some selected values by searching for a tuple with a particular name, like so:</p>
  
  <pre><code>defp get_link([{"link",_link} | _t]), do: _link
  defp get_link([_h | _t]), do: get_link(_t)
  defp get_link([]), do: ""
  </code></pre>
  
  <p>I'm doing this same thing for several separate elements that I want to extract and when I see lots of code that only varies on one parameter, I smell a bad code smell.  I mean code like this:</p>
  
  <pre><code>defp get_link([{"link",_link} | _t]), do: _link
  defp get_link([_h | _t]), do: get_link(_t)
  defp get_link([]), do: ""
  
  defp get_last_updated([{"last updated",_last_updated | _t]), do: _last_updated
  defp get_last_updated([_h | _t]), do: get_last_updated(_t)
  defp get_last_updated([]), do: ""
  
  defp get_creator([{"creator",_creator} | _t]), do: _creator
  defp get_creator([_h | _t]), do: get_creator(_t)
  defp get_creator([]), do: ""
  </code></pre>
  
  <p>smells of violation of the DRY principle to me.  </p>
  
  <p>So I thought I might do something like this:</p>
  
  <pre><code>defp get_json_element([{element_to_get,value_of_element} | _t]), do: _value_of_element
  defp get_json_element([_h | _t]), do: get_json_element(_t)
  defp get_json_element([]), do: ""
  </code></pre>
  
  <p>But assuming that would work (it compiles but I'm not sure that it would do what I want), how do I call it?  I can't call <code>get_json_element(["link", _link])</code> (or at least I don't think I can)  I mean normally I'd pass the list returned by jsx.decode to the various get_X functions but then how do I pass the pattern match expression?  I'm sorry--I don't think I've phrased my question very well but I can't figure out how to phrase it better. </p>
  
  <p>Is this one of those cases where I should use a macro to reduce the repetitious code?  If so, has anyone already written a macro of this sort?</p>
  
  <p>Also wondering if there's a better way to indicate an empty string.  Using "" just doesn't seem the best approach to me.  Or is there another value that I could return?  Maybe return nil?</p>