JSON-hs is a project mainly written in Haskell, it's free.
JSON parser in Haskell
This library pre-dates the standard Haskell JSON library, but they are very similar. The main difference is that this one does not even use any parser combinators.
To use, the function to call is toJSON
:
toJSON :: Monad m => String -> m JSONValue
Very generic, no? But it has worked in production, so I see it with respect.
It provides a helper operator, @@
which has this signature:
(@@) :: Monad m => String -> JSONValue -> m JSONValue
It is like XPath for JSON; in an object like this:
{'country':{'name':'Uganda', 'anthem':'Oh, Uganda'}}
You can access it thus:
case parseJSON jsonString of
Just jsonObject -> case "/country/name" @@ jsonObject of
Just (JStr name) -> name -- That will return "Uganda".
_ -> ""
_ -> ""
The monad returned contains a value (the parsed JSON) of this data type:
data JSONValue = JNum Int | JStr String | JBool Bool | JNull |
JArr [JSONValue] | JObj (Data.Map.Map String JSONValue)
Not as bad as you feared, is it?
These days, there is a standard Haskell JSON library. I put this here because a lot of legacy code from the company (Scyfy Technologies) depends on it. So here it is.