title: json parsing in haskell
body: <p>I'm trying to parse JSON data in haskell. Having gone through a slew of websites, this is the furthest I have been able to get to.</p>
  
  <pre><code>data Address = Address { house :: Integer, street :: String, city :: String, state :: String, zip :: Integer } deriving (Show)
  data Person = Person { name :: String, age :: Integer, address :: Address } deriving (Show)
  
  getName :: Person -&gt; String
  getName (Person n _ _) = n
  
  getAddress :: Person -&gt; Address
  getAddress (Person _ _ a) = a
  
  getState :: Address -&gt; String
  getState (Address _ _ _ s _) = s
  </code></pre>
  
  <p>I write that in a file ex.hs and load it in ghci --></p>
  
  <pre><code>Prelude&gt; import Text.JSON
  Prelude Text.JSON&gt; :load ex
  Main Text.JSON&gt; let aa = "{\"name\": \"some body\", \"age\" : 23, \"address\" : {\"house\" : 285, \"street\" : \"7th Ave.\", \"city\" : \"New York\", \"state\" : \"New York\", \"zip\" : 10001}}"
  ...&gt; decode aa :: Result JSValue
  </code></pre>
  
  <p>It returns</p>
  
  <pre><code>Ok (JSObject (JSONObject {fromJSObject = [("name",JSString (JSONString {fromJSString = "some body"})),("age",JSRational False (23 % 1)),("address",JSObject (JSONObject {fromJSObject = [("house",JSRational False (285 % 1)),("street",JSString (JSONString {fromJSString = "7th Ave."})),("city",JSString (JSONString {fromJSString = "New York"})),("state",JSString (JSONString {fromJSString = "New York"})),("zip",JSRational False (10001 % 1))]}))]}))
  </code></pre>
  
  <p>Needless to say, it seems pretty verbose (and frightening). I tried doing</p>
  
  <pre><code>...&gt; decode aa :: Result Person
  </code></pre>
  
  <p>and it gave me an error. How do I go about populating an instance of the Person datastructure from this json string? For example, what should I do to get the state of the person in the JSON string...</p>