Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse JSON with fieldnames that contain reserved keywords

Tags:

haskell

aeson

I'm trying to parse the following JSON with aeson.

{
    "data": [
        {
            "id": "34",
            "type": "link",
            "story": "foo"
        },
        {
            "id": "35",
            "type": "link",
            "story": "bar"
        }
    ]
}

Since there are a lot of field I'd like to ignore, it seems I should use GHC generics. But how to write a data type definition that uses Haskell keywords like data and type? The following of course gives: parse error on input `data'

data Feed = Feed {data :: [Post]}
    deriving (Show, Generic)

data Post = Post {
        id :: String,
        type :: String,
        story :: String
    }
    deriving (Show, Generic)
like image 384
mb21 Avatar asked Aug 23 '13 19:08

mb21


People also ask

Does JSON have reserved words?

Some property names are reserved for specific uses (e.g. language , global ). Even if the JSON parsing is correct, using reserved words may return the following response: TypeError | cannot read property 'global' of undefined .

How do I parse JSON in Powerautomate?

Using 'parse JSON' action Instead of select operator, if you want to get multiple values from JSON like more than 3 or 4 then we can achieve it using 'Parse JSON' which is a standard connector. Configure parse JSON accordingly. The value of content will be the 'body' value from 'Send an HTTP request to SharePoint.

What is parse in Isjson?

JSON parsing is the process of converting a JSON object in text format to a Javascript object that can be used inside a program. In Javascript, the standard way to do this is by using the method JSON. parse() , as the Javascript standard specifies.

What is JSON parsing example?

Android supports all the JSON classes such as JSONStringer, JSONObject, JSONArray, and all other forms to parse the JSON data and fetch the required information by the program. JSON's main advantage is that it is a language-independent, and the JSON object will contain data like a key/value pair.


1 Answers

You can write your own FromJSON and ToJSON instances without relying on GHC.Generics. This also means that you can use different field names for the data representation and the JSON representation.

Example instances for Post:

{-# LANGUAGE OverloadedStrings #-}
import Control.Applicative
import Data.Aeson
import qualified Data.ByteString.Lazy as LBS

data Post = Post {
        postId :: String,
        typ :: String,
        story :: String
  }
  deriving (Show)

instance FromJSON Post where
  parseJSON (Object x) = Post <$> x .: "id" <*> x.: "type" <*> x .: "story"
  parseJSON _ = fail "Expected an Object"

instance ToJSON Post where
  toJSON post = object 
    [ "id" .= postId post
    , "type" .= typ post
    , "story" .= story post
    ]

main :: IO ()
main = do
  print $ (decode $ Post "{\"type\": \"myType\", \"story\": \"Really interresting story\", \"id\" : \"SomeId\"}" :: Maybe Post)
  LBS.putStrLn $ encode $ Post "myId" "myType" "Some other story"

The same can be done for Feed. If you didn't have to ignore fields, you could also use deriveJSON from Data.Aeson.TH, which takes a function to modify field names as it's first argument.

like image 154
bennofs Avatar answered Oct 05 '22 12:10

bennofs